Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HibernateUtil needed for multiple databases

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I've built a standalone Java/Hibernate/MySQL app where all the tables live in one database witin one instance of MySQL on one physical server. I patterned the app after the canonical caveat emptor app ie HibernateUtil, GenericHibernateDAO, DAOs for all my persistance classes etc. I use annotations in my persistant classes.

Now I must also pull data from additional tables, some living in different databases within the same instance of MySQL on the same server, others in different databases on different instances of MySQL on different physical servers.

I'm aware that each databases needs its own SessionFactory.
I'd like to use the pattern where HibernateUtil has been adjusted to use a HashMap<String, SessionFactory> , googling turns up dozens of variations of this.

But what changes need to be made to the rest of the app?

If database Olaf contains tables Animals, Cars and Trees
and
database Sven contains tables Plants, Planes and Flowers

then I would need an Olaf.cfg.xml and Sven.cfg.xml referencing
their respective tables, and adjust hibernate.cfg.xml to
reference the two .cfg.xml , is that correct?

I think GenericHibernateDAO also needs an adjustment
from:

to


or something.

Sure would've been nice if the JPH book had an example of this common need.

Would anyone point me to a complete, working example that'd be great! But all help and suggestions are most welcome.

TIA,

Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got my HibernateUtil changed around to create a SessionFactory for two connections corresponding to two files:
olaf.cfg.xml
sven.cfg.xml

and stores the two factories in a hashmap<String, SessionFactory> structure.

I've updated my main() to create two separate Sessions:
Session session_a = HibernateUtil.currentSession("olaf");
Session session_b = HibernateUtil.currentSession("sven");
Transaction tx_a = session_a.beginTransaction();
Transaction tx_b = session_b.beginTransaction();

But now I need to know how to modify my HibernateDAOFactory to work with more than one database. Or must I instead use an Olaf_HibernateDAOFactory and Sven_HibernateDAOFactory?

Still-learning Steve
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured this out myself and posted my solution here:


https://forum.hibernate.org/viewtopic.php?f=1&t=1007336&p=2436938#p2436938


CASE CLOSED

Thanks for reading,


Still-learning Steve
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic