Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamic Schema Access in Hibernate

 
k sabeerdeen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Experts,

consider this scenario,

In Mysql Database i have two Schemas:

schema1 - contains table 'employee' (Indian - employees details)

schema2 - contains table 'employee' (Pakistani -employees details )

depend upon user (indian / pakistani ) schema1 or schema2 should be accessed..

i am using hibernate3 + struts

please help me out to achieve this. ( if useful link provided then it will be help ful)


thanks in advance!



 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need two session factories and DAO logic to swap between the two.

 
k sabeerdeen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tips to meet out this situation..

1) Have two xml files
hibernate.config1.xml ( contains connection details for schema1)
hibernate.config2.xml ( contains connection details for schema2)
2)
public void doDBOperation(String type){
......
......
SessionFactory sessionFactory = new Configuration().configure("hibernate.config"+type+".xml").buildSessionFactory();
Session session =sessionFactory.openSession();
Query query = session.createQuery("select s.userName, s.userRole, s.userEmail, s.userPhoneNo from UserHB as s");
.......
}

// if type = 1 (indian)
// if type =2 (pakistani)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Configuring a SessionFactory is a heavy weight operation. You should only do it once (or in your case, twice) in one application. Creating one on the fly to run a query is wasteful and likely to be slow.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic