wood burning stoves*
The moose likes Object Relational Mapping and the fly likes having two hibernate.cfg.xml on the same project Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "having two hibernate.cfg.xml on the same project" Watch "having two hibernate.cfg.xml on the same project" New topic
Author

having two hibernate.cfg.xml on the same project

Natalia Lopez
Ranch Hand

Joined: Feb 17, 2005
Posts: 41
Hi!
It's possible to have two differents configuration files in the same project
one with tne name hibernate.cfg.xml and the other with the name
userDirectorydb.cfg.xml
I have two diferent Singleton clases to make instance of diferents configuration files with the following method

private static SessionFactory createPersistenceManager()
{
try {
System.out.println("Initializing Hibernate with UserDirectory DataBase");
sessionFactory = new Configuration().configure().buildSessionFactory();

SessionFactory sf = new Configuration()
.configure("/com/users/db/userDirectorydb.cfg.xml")
.buildSessionFactory();
// .configure("/hibernate.cfg.xml")


System.out.println("Finished Initializing Hibernate with Userdirectory DataBase");
}
catch (HibernateException e) {
e.printStackTrace();
}
return sessionFactory;
}
But the Singleton always use the same hibernate.cfg.xml even when I change the line .configure("/hibernate.cfg.xml")
for the line .configure("/com/users/db/userDirectorydb.cfg.xml")

Any Idea ? What's wrong?
King Regards

Natalia
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
look here, at the end of the page;
if instead you have HiA, have a look at page 52

let me know if this helps, because as i'm using HibernateUtil i dont see quite clearly as i could easily create a switch between two diferent configurations
[ March 21, 2005: Message edited by: miguel lisboa ]

java amateur
Natalia Lopez
Ranch Hand

Joined: Feb 17, 2005
Posts: 41
Thanks for the link
The last part of the page where say
"You can pick a different XML configuration file using
SessionFactory sf = new Configuration()
.configure("/my/package/catdb.cfg.xml")
.buildSessionFactory();"
That not work, even when you change the catdb.cfg.xml still the project search for the hibernate.cfg.xml
Only take the hibernate.cfg.xml I try delete this file and replace for other but not work
Any idea?
Regards

Natalia
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
hi Natalia

here's what i've found out:
i'm using eclipse; i created a h.cfg.xml file in the same folder where hibernate.cfg.xml is.

then, went to my HibenateUtil.java and passed "/h.cfg.xml" to configure():

so far i've no way of checking this besides within my tests: i have a MySql database for real and a Hsqlbd for tests - hence the need of a switch:

if i want to use MySql (defined in hibernate.cfg.xml) i use HibernateUtil with method configure without any param;

if i want to run faster i use the other cfg, having HibernateUtil as i showed and i also add h.cfg.xml to my test:



So if i have dozens of tests (i do) that need database connection i've to switch manually , or else have two diferent configurations in hibernate.cfg.xml, commenting one or another as i need using one or the other database (which is what i actually do)
[ March 22, 2005: Message edited by: miguel lisboa ]
Natalia Lopez
Ranch Hand

Joined: Feb 17, 2005
Posts: 41
Yes you are right with the solution, I finally found the error!!
Your example work perfectly!
Thanks a lot for your self!

Natalia
 
jQuery in Action, 2nd edition
 
subject: having two hibernate.cfg.xml on the same project
 
Similar Threads
hibernate.cfg.xml not found
problem with hibernate and struts
What hibernate.hbm2ddl.auto value to use?
Collection Mapping
Hibernate Configuration help!