NHibernate and multiple databases

I am trying to solve a fairly easy problem. I want to establish a connection with 2 completely different databases (but also with mysql). Now I tried to solve this problem by creating multiple config files and then creating multiple sessions. Everything works until I reach a relationship.

I have 2 tables in 2 databases:

DB1 - News

db2 - News_Authors

I added news to db1 config and News_Authors to db2 config. When I try to build a simple one-to-one relationship error, I get:

An association from the table songs refers to an unmapped class: db1.News_Authors

      

News.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="project.News, project" table="news" lazy="false">
    <id name="id" column="id" type="integer" length="11">
      <generator class="native" />
    </id>
    <property name="title" type="String" length="255" />
    <property name="authorid" type="integer" length="5" />

    <one-to-one name="NewsAuthor" cascade="all-delete-orphan" lazy="proxy" column="authorid" unique="true" />
  </class>     
</hibernate-mapping>

      

News_Authors.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="project.News_Authors, project" table="news_authors" lazy="false">
    <id name="id" column="id" type="integer" length="11">
      <generator class="native" />
    </id>
    <property name="name" type="String" length="255" />
  </class>     
</hibernate-mapping>

      

CONFIG I added this to enable mappings. Now if I set both in the same config file everything works ...

<mapping resource="project.News.hbm.xml" assembly="project" />

      

So how could I also "notify" nhibernate during session creation that I have multiple sessions? Or should I take a completely different approach?

+1


a source to share


2 answers


Another possible solution is to create views in one of the SQL Server databases to reference tables in the other. Displays a map just like tables, and it's easy to make a view that returns something like:

select * from db2.News_Authors

in database db1.



thus you only need one .hbm.xml file that maps to one of the two databases.

Hope it helps,

-Max

+4


a source


What you need is not multiple sessions, but multiple session plants. For details see.



The key point here is that you don't need to initialize the factory session through the config file - you can do it programmatically. And this is just a step towards creating two session factories.

0


a source







All Articles