-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 3:28 am 
Newbie

Joined: Mon Oct 13, 2008 8:56 am
Posts: 4
How do I create association between multiple databases or catalogs.
I have created two different session factories for two databases:
Code:
<bean id="objSessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="objDataSource1"/>
        </property>
       
        <property name="mappingResources">
            <list>
                <value>Test1.hbm.xml</value>
...

Code:
<bean id="objSessionFactory2" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref local="objDataSource2"/>
        </property>
       
        <property name="mappingResources">
            <list>
                <value>Test2.hbm.xml</value>
...


While creating association <many-to-one> I am getting error that
Code:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'objSessionFactory2' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table refers to an unmapped class:

Please help as I need to use two different databases.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 5:07 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
You want to map tables in two different databases with an association via mapping files? You know how Hibernate works? It generates SQL-statements and executes them via JDBC. What database can handle a SELECT that uses tables "living" in another DB? That cannot work.

You have to build the association inside your app. Read from the first DB. Get the values you need and go to the second DB. Put it in your model. But there cannot be an automagic solution.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 6:55 am 
Newbie

Joined: Mon Oct 13, 2008 8:56 am
Posts: 4
Multiple db can be handled from single query. Have you ever tried this.
Please check this query in mysql. It runs smoothly. I want to execute this from Hibernate.
You can excute this query from any of the database.
Code:
select a.fld1,b.fld2 from db1.tbl1 a,db2.tbl2 b where a.fld3=b.fld4

CDillinger wrote:
You want to map tables in two different databases with an association via mapping files? You know how Hibernate works? It generates SQL-statements and executes them via JDBC. What database can handle a SELECT that uses tables "living" in another DB? That cannot work.

You have to build the association inside your app. Read from the first DB. Get the values you need and go to the second DB. Put it in your model. But there cannot be an automagic solution.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 7:04 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
The notation you posted gets data from different schema not databases! You cannot run a query in one database and get data from a completly different other db. What you can do is using database links e.g. in Oracle. But this leads to firing the statement against ONE database instance.

If you only want to use to different schema inside one database you can use the qualified tablename, but you don't need two SessionFactory then.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 7:30 am 
Newbie

Joined: Mon Oct 13, 2008 8:56 am
Posts: 4
I have tried this query with two different databases and not schemas.Please check before posting again. I have executed it in mysql and looking for the same through Hibernate.

The same query is also possible through HQL but how to make it through mapping is a problem.
Code:
from Class1 a,Class2 b where a.fl1=b.fld2

The above classes refer to two different databases i.e. different SessionFactory Objects.

CDillinger wrote:
The notation you posted gets data from different schema not databases! You cannot run a query in one database and get data from a completly different other db. What you can do is using database links e.g. in Oracle. But this leads to firing the statement against ONE database instance.

If you only want to use to different schema inside one database you can use the qualified tablename, but you don't need two SessionFactory then.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 7:41 am 
Senior
Senior

Joined: Tue Oct 28, 2008 10:39 am
Posts: 196
You asked for help not me, so no reason to stamp on the ground and talk to people that try to help YOU like the way you do.

You could post your SessionFactory config.
How can a MySQL DB know another MySQL DB on another server? Maybe your statement works if you connect to MySQL and execute this statement and MySQL-engine can connect the resultsets, but Hibernate does not know anything about the physics of your DB instances and cannot know if this obviously MySQL specific feature can work with the configuration you use.


Top
 Profile  
 
 Post subject: Re: Create Association between multiple databases
PostPosted: Fri Jan 22, 2010 7:49 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You need to use one SessionFactory only. Then, in the mapping files for your classes, use the schema or catalog attributes. For example:

Code:
<class name="Test1" table="Test1" catalog="...">

<class name="Test2" table="Test2" catalog="...">


This of course assumes that the databases lives in the same MySQL server.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.