aspose file tools*
The moose likes Object Relational Mapping and the fly likes Dynamic Schema Access in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Dynamic Schema Access in Hibernate" Watch "Dynamic Schema Access in Hibernate" New topic
Author

Dynamic Schema Access in Hibernate

k sabeerdeen
Greenhorn

Joined: Mar 11, 2010
Posts: 14
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

Joined: Apr 14, 2004
Posts: 10336

You'll need two session factories and DAO logic to swap between the two.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
k sabeerdeen
Greenhorn

Joined: Mar 11, 2010
Posts: 14
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

Joined: Apr 14, 2004
Posts: 10336

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Schema Access in Hibernate