Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Hibernate configs

Tags:

java

hibernate

I'm currently working on building a library to modularize some of my code and I'm running into a problem with Hibernate.

In my main application I have a hibernate config to get information it needs to run but then I also have a need for hibernate in my library since some of the objects I want could be used in other applications.

When I start up my tomcat server, with both hibernates setup, I get errors stating that beans cannot be resolved and one that says my positional parameters are missing in my query. However, when I start up Tomcat with only the application Hibernate config it starts fine.

Here's what the configs look like...

From the library:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>


<session-factory>   
    <mapping resource="blah.hbm.xml"/>
    <mapping resource="blargh.hbm.xml"/>
    <mapping resource="stuff.hbm.xml"/>
    <mapping resource="junk.hbm.xml"/>
    <mapping resource="this.hbm.xml"/>
</session-factory>

</hibernate-configuration>

And from the application:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>


<session-factory>       

    <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

    <!-- Enable the query cache  -->
    <property name="hibernate.cache.use_query_cache">true</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>

    <!-- mapping files -->

    <mapping resource="appStuff"/>
    <mapping resource="appBlah"/>
    <mapping resource="appBlargh"/>
    <mapping resource="appJunk"/>
    <mapping resource="appThis"/>    

</session-factory>

</hibernate-configuration>

I'm still pretty new to Hibernate and this is sort of a weird configuration.

like image 900
Shaded Avatar asked Aug 10 '10 13:08

Shaded


People also ask

Can we have multiple configuration files in Hibernate?

You can load hibernate configuration files programatically.

Can we have multiple Hibernate CFG XML?

Yes, you can.

Can we have multiple SessionFactory in Hibernate?

Q. What we can create more than one sessionFactory in Hibernate. Ans And Its true We can create.As in my app i am able to da same.

Is it necessary to use Hibernate CFG XML for configuration?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.


1 Answers

You can load hibernate configuration files programatically.

SessionFactory sf = new Configuration().configure("somename.cfg.xml").buildSessionFactory();

That would allow you to create two SessionFactory objects. However, I assume that you want to use the same SessionFactory for your app and your module.

You could load both hibernate XML files into a single DOM object (combine your module's "session-factory" tag children with your application's ones) and then use the following code:

import org.hibernate.cfg.Configuration;
// ...
SessionFactory sf = new Configuration().configure(yourDOMObject).buildSessionFactory();

Edit: session-factory wasn't printed because it had greater-than and less-than characters.

like image 156
Tiago Alves Avatar answered Oct 20 '22 00:10

Tiago Alves