-->
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.  [ 6 posts ] 
Author Message
 Post subject: Accessing multiple tables of two different databases
PostPosted: Sat May 17, 2008 7:33 am 
Newbie

Joined: Sat May 17, 2008 5:22 am
Posts: 8
I am new to Hibernate. I have done few basic examples in Hibernate.
I have a doubt could anyone clarify my doubt.

I have two databases namely Database 1 and Database 2.
Database 1 contains a table called Table Source. And table Source has an id and value columns like this
Table Source

Id1 Value1

1 A
2 B
3 C

Database 2 contains a table called Table Destination. And the table Destination has two fields like this

Table destination

Id2 Value2

1
2
3
1
2
3

I have separate hibernate.cfg.xml files for each of the databases configuration, from which I am able to get the session factory and session of each of the databases seperately.
Now I want to write a query to update the destination value field with the values of the of the Source Value field of the Source table.
I can write a query like this “update destination set destination.value2 = (select source.value1 from Source where Source.id1= Destination.id2) “.

I am able to execute the above query when the tables are in the same database. But I am not able to execute the query when the tables are in separate databases.

Now my question is that is there any way we can combine the sessions of these two databases so that I am able to update the tables. If not combining the sessions is not possible can any one suggest any other solution for this problem.
At the End my destination table should look like this
Id2 Value2
1 A
2 B
3 C
1 A
2 B
3 C


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 17, 2008 3:27 pm 
Newbie

Joined: Fri May 16, 2008 3:40 pm
Posts: 13
You should create a database link, importing one table into the other database. In your example, import the table destination into database 1 and you can run the query on database 1.

I don't know if all rdbms's support database links. Oracle does.

Which dbms are you using?

And, why are the tables on two different databases if they belong to the same application?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 17, 2008 10:30 pm 
Newbie

Joined: Sat May 17, 2008 5:22 am
Posts: 8
I am MSSQL 2000. No application is structured in a way that the two tables are in different tables. Cant i achieve this my HQL or any other means ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 17, 2008 10:30 pm 
Newbie

Joined: Sat May 17, 2008 5:22 am
Posts: 8
I am using MSSQL 2000. My application is structured in a way that the two tables are in different databases. Cant i achieve this by HQL or any other means ?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 18, 2008 8:55 am 
Newbie

Joined: Fri May 16, 2008 3:40 pm
Posts: 13
Maybe you have a wrong view about what Hibernate does. Hibernate does not execute HQL queries. Instead it translates HQL queries into SQL queries, possibly taking into account specifics of the used database system. The SQL queries are executed by the DBMS the results are translated again by Hibernate (from table rows to objects).

Now if you cannot have the query being executed in SQL on a DB, you will not find a way to execute it in HQL from hibernate. Thus my point: Make sure that one of your databases is able to execute the query you want by making both tables available to it (i.e. create a db link to make it access the remote table).

A (not-recommended) alternative would be to query each table separately and then do the join in your JAVA code. Error prone and possibly horribly slow.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 18, 2008 9:56 am 
Newbie

Joined: Sat May 17, 2008 5:22 am
Posts: 8
Thanks mate. Yes i am new to hibernate and HQL. Ok is it possible to write an update query if i had the source data of the table in a HashMap. Now i have a hashmap and i need to update a table with it. And the hashmap is smilar to the table 1 which i explained in the top.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.