[ActiveRecord - multiple db] relation accross different databases?

3 views
Skip to first unread message

Gauthier Segay

unread,
Sep 5, 2009, 6:42:54 AM9/5/09
to Castle Project Users
I'm using ActiveRecord with multiple databases in single application
with great success,

Until now, I always had clear boundary context for theses separations
but I'm looking for a mapping level solution (if available) to this
need:

I'm talking about 2 readonly databases (where I exposed bare SQL
views), I would like to have a BelongsTo relation from an entity
defined in DB1 to another entity defined in DB2.

I'm not sure that NHibernate has ways to handle this, I just tried for
the heck of it as a first spike to solve my need and I'm getting the
following exception:

MappingException: An association from the table {tablenamefromdb1}
refers to an unmapped class: {TypeFromDB2BelongingToEntityFromDB1}

I've tried to swap mapping configuration order to make db2 registered
before db1 but the error is the same.

I'm wondering if some ActiveRecordStarter.ModelCreated magic could
overcome this exception.

I'll understand if such thing is not possible, so I'm leaning to
migrate this concern on my applicative service layer, so I would also
like to gather experiences from other users in perspectives of
possible solutions / experiences.

Thanks for your insight

Mauricio Scheffer

unread,
Sep 5, 2009, 10:38:14 AM9/5/09
to Castle Project Users
Did you try mapping to a view that selects from the other database?
i.e. in DB1 you:

create view EntityFromDB2 as select blablabla from db2..entity

and you map nhibernate to EntityFromDB2

Gauthier Segay

unread,
Sep 5, 2009, 12:14:45 PM9/5/09
to Castle Project Users
Hello Mauricio, thanks for your suggestion.

I've tought about that, but actually I'm using two different engines/
servers: DB1 is mysql, DB2 is postgre

I know mssql (which is the DB0 off the bat ;)) has a feature known as
"linked server", but I think it wouldn't be simple to use in my case
(need to scavenge odbc drivers that will work on win64 etc.)

I suppose I'm getting to work around this in my applicative service
layer.
Reply all
Reply to author
Forward
0 new messages