The moose likes Object Relational Mapping and the fly likes How to connect multiple databases at a time using Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "How to connect multiple databases at a time using Hibernate" Watch "How to connect multiple databases at a time using Hibernate" New topic
Author

How to connect multiple databases at a time using Hibernate

v chenna
Greenhorn

Joined: Dec 28, 2009
Posts: 11
I am developing Hibernate application which it has to support to connect multiple databases with out changing the code.

For example, client has his own choice to select a data base(Oracle,SQL Server,My SQL...) code has to support.

please provide the code or any link or clue for this

bye.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4932

The DAO pattern makes it very easy to create an application that can use multiple databases, with minimal changes to support that. So, if you're using DAOs, it shouldn't be too difficult a switch.

Advanced DAOs with Hibernate


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
v chenna
Greenhorn

Joined: Dec 28, 2009
Posts: 11
please can you provide the code or link for that using Hibernate not DAO,
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4932

Take another look at that page. It's all in Hibernate.

-Cameron McKenzie

This message was edited 1 time. Last update was at by Cameron Wallace McKenzie

Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Posts: 18

you can get something to spring and hibernate to connect to multiple datasource
Justin Chi
Greenhorn

Joined: Sep 09, 2009
Posts: 25
v chenna wrote:I am developing Hibernate application which it has to support to connect multiple databases with out changing the code.

For example, client has his own choice to select a data base(Oracle,SQL Server,My SQL...) code has to support.

please provide the code or any link or clue for this

bye.


This can not be done only by configuration.

You will need multiple hibernate DB configuration files per database like hibernate.oracle.cfg.xml, hiberante.mysql.cfg.xml , hiberante.sqls.cfg.xml , etc.

Then there better be a property file (db.properties) telling which db is being used by application.

Created a class DBBundle ( extends java.util.ResourceBundle) mapping to db.properties, suppose your configuration in db.properties is myDB=oracle.

Then can map to db as below :

if ("oracle".equals(DBBundle.myDB){
new Configuration().configure("hibernate.oracle.cfg.xml").buildSessionFactory();
}else if ("mysql".equals(DBBundle.myDB){
new Configuration().configure("hibernate.mysql.cfg.xml").buildSessionFactory();
}else if (.....){
.....
}else{
// throw exception ......
}

And I am sure you can make it more configurable.

This message was edited 4 times. Last update was at by Justin Chi

v chenna
Greenhorn

Joined: Dec 28, 2009
Posts: 11
Thanks Justin for providing valuable information. If you don't mine can i get complete code or related material.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4932

If you don't mine can i get complete code or related material.


This is all standard DAO pattern and Hibernate stuff. It would appear that the problem is not the lack of code examples being provided to you. From the links provided, I think you have been given more than enough information to start building your own program.

Try out some code, and see what works and what doesn't. Ask us question about problems you're having, and we'll help.

From the difficulty you're having understanding the code samples already provided, I'd say that you're probably having some trouble with basic Hibernate and DAO concepts. Learn the fundamentals of Hibernate, and work through that DAO tutorial that I provided - all of the code is there. Please give it a try, do some coding of your own, and tell us where you're having problems. We can't actually code a solution to your problem for you.

-Cameron McKenzie

 
 
subject: How to connect multiple databases at a time using Hibernate
 
developer file tools