0

I know it's possible to configure Hibernate in the 2 following ways:

  1. hibernate.cfg.xml

  2. persistence.xml - JPA - with specific hibenate configuration when you add the provider like this:

    org.hibernate.ejb.HibernatePersistence

But , I don't understand when should I use which ? What should be the correct behavior? Thanks!

1 Answer 1

2

You can use hibernate.cfg.xml when you want to use Hibernate in your project. When you create different queries (SELECT, INSERT, etc) you open session.

Session session = sessions.openSession();

where session is instance of org.hibernate.Session. But as you can see we need sessions for creating session. Sessions is instance of org.hibernate.SessionSessionFactory. SessionFactory is global factory for concrete DB. Session can be got by this action:

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

where new Configuration().configure().buildSessionFactory() - parses hibernate.cfg.xml

Persistence.xml contains settings for using JPA in your project. Persistence units are defined in a persistence.xml file, which has to be located in the META-INF directory in the classpath. One persistence.xml file can include definitions for one or more persistence units. The portable way to instantiate an EntityManagerFactory in JPA (as explained in the JPA Overview section) requires a persistence unit.

See also: http://www.objectdb.com/java/jpa/entity/persistence-unit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.