The most intelligent Java IDE
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
Author

How to connect multiple databases at a time using Hibernate

v chenna
Greenhorn

Joined: Dec 28, 2009
Messages: 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
Bartender

Joined: Aug 26, 2006
Messages: 4586

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???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

v chenna
Greenhorn

Joined: Dec 28, 2009
Messages: 11

please can you provide the code or link for that using Hibernate not DAO,
Cameron Wallace McKenzie
author and cow tipper
Bartender

Joined: Aug 26, 2006
Messages: 4586

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


Author of Hibernate Made Easy, What is WebSphere???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

Ernesto Chicas
Greenhorn

Joined: Jan 13, 2010
Messages: 8

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

Joined: Sep 09, 2009
Messages: 13

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
Messages: 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
Bartender

Joined: Aug 26, 2006
Messages: 4586

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


Author of Hibernate Made Easy, What is WebSphere???, Portlet Programming Made Easy and the SCJA Certification Guides
My Hibernate and JPA Tutorials * My Mock Java Certification Exams * My Online Java Tutorials * My CBT Portlet Tutorials.

 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
IntelliJ open source