Alex
Alex

Reputation: 3405

Hibernate Eclipse WebProject configuration: How to specify file config?

Consider the following statement:

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

Where:

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

How to specify a file from which Hibernate will retrieve configuration data? Explaining, I have a hibernate.cfg.xml with proper configuration data, but during runtime hibernate throws errors refering to configurations from another projects, like:

org.hibernate.MappingNotFoundException: resource: xx/AnotherNonRelatedProject/CertainClass.hbm.xml not found

Anyway, I'm guessing that default configuration is comming from another file. But I've searched my hbm.xml files, classes and references, and it seems to be ok.

Any idea on what would be happening here?

Eclipse: Indigo SR2; Hibernate Tools: 3.5.1

Thanks!

Upvotes: 0

Views: 314

Answers (2)

Alex
Alex

Reputation: 3405

I'm responding to my own question because I've discovered my specific problem, which I think would be the another one's problem on question's matter:

As my application is Web-based, Tomcat's lib folder has a .jar file from another project which had hibernate configuration problems. As Hibernate checks all dependencies related to some project (including all .jar), which includes all jars in Tomcat's lib. So, there were exceptions (totally strange to me at that time) raising during runtime. Solving hibernate configuration problems of .jar files or deleting them (if not necessary) from Tomcat's lib folder solves the problem.

Upvotes: 0

Marcelo Tataje
Marcelo Tataje

Reputation: 3881

if you want to set your own Hibernate configuration, then you must use the following:

private static final SessionFactory sessionFactory;
    static {
        try {
            // Create your SessionFactory from hibernate.cfg.xml
            sessionFactory = new Configuration().configure(new File("hibernate1.cfg.xml"))
                    .buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

Best regards :)

Upvotes: 1

Related Questions