views:

43

answers:

3

Good Morning everyone!

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"&gt;

<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"&gt;

<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 sorry if this is a really stupid question but I'm still pretty new to Hibernate and this is sort of a weird configuration.

Thank you for any suggestions and/or answers!

A: 

Can't you just use the application hibernate config then?

Jeroen Rosenberg
@Jeroen Rosenberg: What do you mean? If I wanted to use my library in a different app wouldn't it be better if it had it's config already setup and didn't require any extra code on the app end?
Shaded
+3  A: 

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.

Tiago Alves
@Tiago Alves: This seems to be what I'm looking for, but let me ask this. Would it be better to let my library have it's own session factory that it can use, or should I have one session factory for the whole thing? For my situation it seems multiple session factories would be more appropriate, but I don't know if I'll run into trouble if I go down that path.
Shaded
To be honest, I don't have that much experience with Hibernate. However, from what you describe, I think multiple SessionFactory objects seem appropriate for your use case as it helps in modularizing your code. But don't take my word for it as I don't even know if there could be some performance impact, for instance or any other trouble.
Tiago Alves
@Tiago Alves: Thanks for the help, I'm pretty sure multiple is the way to go. I'm gonna run with it and hope it doesn't come back to bite me!Thanks again!
Shaded
A: 

if you want to do it propely use hibernate shard 1. Othewise you can simply pass the path (on file system or in classpath ) of the hibernate.cfg.xml you want to use

From the library

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

And from the application:

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