• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Hibernate.cfg.xml problems

 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a very sticky problem for all Hibernate Gurus.

I have built a maven, hibernate project in eclipse wich uses annotations and the famous HibernateUtil class wich can be found at hibernate.org. The project handles Languages, Users and Markets.

This project is included in another project altso hibernate, maven that handles another task.

My problem here is that when Hibernate gets the SessionFactory it initiates all Hibernate.cfg.xml in the classpath. ( I have one in every project ).

I must be able to have multiple Hibernate projects imported to another project. How can I come around this?

The projects are api wich is imported to other projects. One may use the api in a webapplication. The same api can be imported in another app and so on. What happens if the Webapplication edits a Language. How will this affect other projects that has imported the Language, User api.

Hope you understand what I meen. I'm tearing my hair here, at least was left of it.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use more than one SessionFactory, and for each of them, specify a configuration file.
[ August 23, 2007: Message edited by: Edvins Reisons ]
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I only want one. My problem is that when a jar with a hibernate.cfg.xml file is included in a project, there are 2 configuration files in the classpath.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you create a SessionFactory, you can specify the name of a configuration file to use. Is this what you are looking for?
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, maybe..... but this meens that I must have one HibernateUtil class per product and can not have one in my common jar right?
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want your projects packaged separately, then yes, a way to do this
may be to have HibernateUtil1, HibernateUtil2,..., and for each of them,
specify a configuration to have a SessionFactory built.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I will go with this stratergy. Create separate HibernateUtil class for each project and named hibernate configurations.

// Mathias
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead why not pass the filename as a parameter through the env-entry in each of the web.xml? You wont need 2 separate HibernateUtil in that case. Your HibernateUtil can pick up the configuration file name from this env-entry.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a good id� but I use hibernate as pure libs as well. Just annotated classes with no web gui. The libs may be used separately or together. Schemas can have foreign key to other schemas.

For example I have a Central lib where all users, markets etc gets created for all applications. We use the same user table for all applications.
This package is distributed in 10 - 100 other projects and sometimes mapped to each other.

The central lib need one configuration and the other projects other configurations. Hope you understand what I meen
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, i understand what you are saying

This package is distributed in 10 - 100 other projects and sometimes mapped to each other.

The central lib need one configuration and the other projects other configurations


Hmmm... This might mean 10-100 different HibernateUtils (one for each project), isnt it?
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes and I don't think this is the ultimate solution. I meen every hibernate guy out there must have own hibernate jars in other projects they have created. Common jars, central jars etc used in other projects?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mathias Nilsson:
That is a good id� but I use hibernate as pure libs as well. Just annotated classes with no web gui. The libs may be used separately or together. Schemas can have foreign key to other schemas.

For example I have a Central lib where all users, markets etc gets created for all applications. We use the same user table for all applications.
This package is distributed in 10 - 100 other projects and sometimes mapped to each other.

The central lib need one configuration and the other projects other configurations. Hope you understand what I meen [/QB]


OK, a bit more thinking, and i still believe that you wouldnt require multiple HibernateUtils. Here's what you could do:

- Have a single HibernateUtil which looks for a file named "app.properties" (name it whatever sounds good)
- Each project(including the central project) will have a separate app.properties. This app.properties will mention the path/filename of the hibernate configuration file.
Ex: Central project's app.properties might mention central-hibernate-cfg.xml in this file. Whereas project1 might mention proj1-hibernate-cfg.xml in this file. And so on for each project.

With this approach you are going to end up with n number of app.properties file (1 each for each project) but a single HibernateUtil file. This approach looks better than having multiple HibernateUtils. After all that's what the properties files are meant for.

Does this sound feasible?
 
Michele Mercurio
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jaikiran Pai:
... Each project(including the central project) will have a separate app.properties. This app.properties will mention the path/filename of the hibernate configuration file.
...
With this approach you are going to end up with n number of app.properties file (1 each for each project) but a single HibernateUtil file. ...


Hi,
I have a similar problem:

1 core web application with its (main) hibernate.cfg.xml +
3 dependent sub-projects (sub1, sub2, sub3) with their 3 hibernate.cfg.xml.

I want to take the 3 sub-projects separated so I need to split the actual main in the final 1 + 3 files.

A single hibernate.cfg.xml declare only the resource maps: HibernateUtil read the db configuration from another property file.

In brief:

sub1 needs main.cfg.xml + sub1.cfg.xml
sub2 needs main.cfg.xml + sub2.cfg.xml
sub3 needs main.cfg.xml + sub3.cfg.xml

if the final product is composed using sub1 + sub3 it needs main.cfg.xml + sub1.cfg.xml + sub3.cfg.xml

Your solution is good if the projects run one at once, but not if we want an additivity feature.

Regards

[ August 30, 2007: Message edited by: Michele Mercurio ]
[ August 30, 2007: Message edited by: Michele Mercurio ]
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jaikiran Pai!

This sounds like a good ide. I will try it and see if it fits.

Thanks
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michele Mercurio:
Hi,
I have a similar problem:

1 core web application with its (main) hibernate.cfg.xml +
3 dependent sub-projects (sub1, sub2, sub3) with their 3 hibernate.cfg.xml.

I want to take the 3 sub-projects separated so I need to split the actual main in the final 1 + 3 files.

A single hibernate.cfg.xml declare only the resource maps: HibernateUtil read the db configuration from another property file.

In brief:

sub1 needs main.cfg.xml + sub1.cfg.xml
sub2 needs main.cfg.xml + sub2.cfg.xml
sub3 needs main.cfg.xml + sub3.cfg.xml

if the final product is composed using sub1 + sub3 it needs main.cfg.xml + sub1.cfg.xml + sub3.cfg.xml

Your solution is good if the projects run one at once, but not if we want an additivity feature.



Michele,

Welcom to JavaRanch

True. If you want additive kind of feature then this is not going to work. The configure method on the Configuration class in Hibernate does however have an overloaded method which accepts

public Configuration configure(org.w3c.dom.Document document) throws HibernateException


So as long as you can create a proper Document object out of those 2 xml files (1 main and 1 sub cfg file) and pass it to this method, you should still be able to accomplish your requirement. The only (maybe) ugly part would be to parse those 2 hibernate configurations file in your application and create a valid Document object out of those.
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another second thought on the subject: the HibernateUtil class is the most useful when all you need is a single SessionFactory with a default configuration. When the picture gets a bit more complex, you have to look inside this class and understand how it works. And once you are through this, you can create your SessionFactory objects, configurations, � , in any suitable way, for example, create in one place all the SessionFactory objects that you need.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did anyone solv this?

I'm still having problems on how to get around this.

// Mathias
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic