• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to connect multiple databases at a time using Hibernate

 
v chenna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
v chenna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please can you provide the code or link for that using Hibernate not DAO,
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take another look at that page. It's all in Hibernate.

-Cameron McKenzie
 
Ernesto Chicas
Greenhorn
Posts: 18
Debian Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can get something to spring and hibernate to connect to multiple datasource
 
Justin Chi
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
v chenna
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic