-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Can we Write more than 2 hibernate.cfg.xml's?
PostPosted: Fri Jan 02, 2009 2:18 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
Hi,
Can we Write more than 2 hibernate.cfg.xml's?
If yes, How to add sessions? please give with exampls?


This code rite? please check..if wrong.. how can we use 2 cfg.xml's?

Configuration cfg = new Configuration();
cfg.addFile("hibernate.cfg.xml");
cfg.addFile("hibernate2.cfg.xml");

sessionFactory = cfg.buildSessionFactory();


Or


Please check this code.. Is it write..?
Is it Possible to use morethan one cfg xmls?

Configuration cfg = new Configuration();
cfg.configure("hibernate1.cfg.xml");
cfg.configure("hibernate2.cfg.xml");
// sessionFactory = new Configuration().configure().buildSessionFactory();
sessionFactory = cfg.buildSessionFactory();


or

?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 02, 2009 10:53 am 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
SessionFactory sourceSessionFactory1 = new Configuration().configure("one.cfg.xml")
.buildSessionFactory();
SessionFactory sourceSessionFactory2 = new Configuration().configure("two.cfg.xml")
.buildSessionFactory();

These may be used to connect to two different databases. I used it to read from TABLE1 and write to TABLE2 in the same database. But I don't think it is possible for one factory to have multiple configurations.

-------------------------
remember to rate


Top
 Profile  
 
 Post subject: can i use more than one Hibernate.cfg.xml file?
PostPosted: Mon Jan 05, 2009 8:07 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
Hi..

its not working.. pls give me other solution.. please.....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2009 9:20 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Configuration config=new Configuration();
config.configure("/hibernate.cfg.xml");
config.addResource("Report1.hbm.xml");
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
Transaction trx=session.beginTransaction();
Iterator results=session.createQuery("from Report1 r").iterate();
while(results.hasNext())
{
Report1 r=(Report1)results.next();
System.out.println(r.getName());
}

Configuration config1=new Configuration();
config.configure("/hibernate1.cfg.xml");
config.addResource("Report2.hbm.xml");
SessionFactory sf=config.buildSessionFactory();
Session session=sf.openSession();
Transaction trx=session.beginTransaction();
Iterator results=session.createQuery("from Report2 r").iterate();
while(results.hasNext())
{
Report2 r=(Report2)results.next();
System.out.println(r.getName());
}


**) don't forget to build ur sessionFactory for ur new configuration.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject: I have no.of ConnectionPools/Datasources.
PostPosted: Tue Jan 06, 2009 2:22 am 
Newbie

Joined: Fri Jan 02, 2009 2:14 am
Posts: 15
HI All,

I have no.of ConnectionPools/Datasources.. morethan 3..
Problem is that.. we need to use these all ConnectionPools/Datasources..
How? in Hibernate..... Pls need urgent.....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 8:44 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
just create new Configuration object with these resources....

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 8:52 am 
Regular
Regular

Joined: Wed Oct 15, 2008 6:59 am
Posts: 103
Location: Chennai
Configuration config = new Configuration()
.addClass(A.class)
.setProperty("hibernate.connection.driver_class", "...")
.setProperty("hibernate.connection.url", "...")
.setProperty("hibernate.connection.username", "...")
.setProperty("hibernate.connection.password", "...")
;
SessionFactory f = config.buildSessionFactory();
Session s = f.openSession();
Transaction tx = s.beginTransaction();


just change that property and build new SessionFactory.

_________________
If u feel it will help you, don't forget to rate me....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2009 12:27 pm 
Regular
Regular

Joined: Tue Dec 30, 2008 8:14 pm
Posts: 50
It is also possible to specify the database connection properties in configuration files. Assuming there are two databases (DB_1 and DB_2), we may create two hibernate configuration files (hibernate1.cfg.xml and hibernate2.cfg.xml) for each of these. Then we can create two session factories using these hibernate configurations; and then create sessions from these factories.

hibernate1.cfg.xml example for oracle:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:oci:@???</property>
<property name="hibernate.connection.username">???</property>
<property name="hibernate.connection.password">???</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
...
</session-factory>
</hibernate-configuration>

hibernate2.cfg.xml for another database:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">???</property>
<property name="hibernate.connection.url">???</property>
<property name="hibernate.connection.username">???</property>
<property name="hibernate.connection.password">???</property>
<property name="dialect">???</property>
...
</session-factory>
</hibernate-configuration>


To use a connection pool supplied by the application server:
<property name="hibernate.connection.datasource">jdbc/???</property>


SessionFactory sessionFactory1 = new Configuration().configure("hibernate1.cfg.xml")
.buildSessionFactory();
SessionFactory sessionFactory2 = new Configuration().configure("hibernate2.cfg.xml")
.buildSessionFactory();

Session session1 = sessionFactory1.openSession();
Session session2 = sessionFactory2.openSession();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.