Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is persistence.xml requied when working with Spring and Hibernate?

I am using a project with Spring JPA and Hibernate. Most of the things in persistence.xml can be specified in Spring applicationContext.xml file.

So is the persistence.xml required anymore?

Thanks.

like image 775
user373201 Avatar asked Jan 19 '11 14:01

user373201


People also ask

Does Spring data JPA require persistence xml?

So, actually you can configure JPA in Spring without persistence. xml by writing a custom PersistenceUnitManager , though such a manager is not available out of the box.

What is persistence xml Hibernate?

The persistence. xml file must define a persistence-unit with a unique name in the current scoped classloader. The provider attribute specifies the underlying implementation of the JPA EntityManager. In JBoss AS, the default and only supported / recommended JPA provider is Hibernate.

What is the purpose of the persistence XML file?

The persistence. xml configuration file is used to configure a given JPA Persistence Unit. The Persistence Unit defines all the metadata required to bootstrap an EntityManagerFactory , like entity mappings, data source, and transaction settings, as well as JPA provider configuration properties.

Where should I put the persistence xml?

xml should be put in the EJB JAR's META-INF directory. If you package the persistence unit as a set of classes in a WAR file, persistence. xml should be located in the WAR file's WEB-INF/classes/META-INF directory.


3 Answers

Update: Spring 3.1 will support persistence.xml-free JPA configuration, see Spring 3.1 M2: Configuration Enhancements.


darioo's answer is good for practical use, but not technically correct.

PersistenceProvider has two factory methods:

  • EntityManagerFactory createEntityManagerFactory(String emName, Map map) - for standalone environments, persistence.xml is to be parsed by persistence provider.

  • EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) - for application server environments, persistence.xml was parsed by application server and its contents is passed as PersistenceUnitInfo.

Spring's LocalContainerEntityManagerFactoryBean emulates the application server environment. Therefore it parses persistence.xml itself, merges its contents with the values from application context, and passes it to the persistence provider using the second factory method.

However, process of obtaining persistence.xml data is configurable:

  • You can configure the name of persistence.xml file using persistenceXmlLocation property - it's useful to avoid conflicts with the default JPA initialization strategies of application servers.

  • You can completely override the source of PersistenceUnitInfo by setting a custom PersistenceUnitManager strategy.

So, actually you can configure JPA in Spring without persistence.xml by writing a custom PersistenceUnitManager, though such a manager is not available out of the box.

like image 104
axtavt Avatar answered Sep 18 '22 19:09

axtavt


persistence.xml is needed when you're using Hibernate through JPA, even though you're using Spring JPA. If you're using Hibernate directly, then persistence.xml isn't needed.

like image 20
darioo Avatar answered Sep 18 '22 19:09

darioo


The JPA specification does not state anywhere that the file is required, but in the definitions of the contracts of EntityManagerFactory and EntityManager and practically throughout the whole specification, the persistence.xml file is mentioned over and over.

I recently had to deal with the question of whether it is possible to programatically configure JPA 2.0 with Hibernate 3.6 without using persistence.xml at all.

I concluded that this is not possible, although you can configure the file to contain a very minimum configuration. I determined that the file must at least contain the name of your persistence unit. The rest of the info can be programatically provided to the entity manager factory as parameters.

I have never used spring though, therefore I do not know if it uses any tricks to overcome this issue.

like image 33
Edwin Dalorzo Avatar answered Sep 18 '22 19:09

Edwin Dalorzo