Is it possible to get rid of persistence.xml when using Hibernate using Spring?

I am always looking for ways to eliminate redundant code from my project.

I am using Hibernate with Spring. I set up Spring JPA LocalContainerEntityManagerFactoryBean, which allows you to pass a bunch of properties to the JPA provider (Hibernate), which means I don't need to set these properties in the persistence.xml file.

In addition, Hibernate can find my constant classes without actually defining them in the persistence.xml file, so those <class> lines also go away.

I am left with one line in my persistence.xml:

<persistence-unit name="bigDatabase" transaction-type="RESOURCE_LOCAL"/> 

Does anyone know a way to completely eliminate persistence.xml and transfer the definition of the unit of duration to the Spring configuration file? Of course, I tried Google, but could not find the answer.

Thanks!

+4
source share
1 answer

persistence.xml not required for JPA configurations. You can create your object manager using the EntityManagerFactory.createEntityManager (map) method. But LocalContainerEntityManagerFactoryBean has no methods to help create an object manager with a map. Therefore, you need to extend LocalContainerEntityManagerFactoryBean and override some methods to suit your needs.

+2
source

All Articles