4
votes

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

I am using Hibernate with Spring. I have configured Spring's JPA LocalContainerEntityManagerFactoryBean, which lets me pass a bunch of properties to the JPA provider (Hibernate), which means I don't need to set those properties in the persistence.xml file.

Furthermore, Hibernate can find my persistent classes without my having to actually identify them in persistence.xml, so those <class> lines go away too.

I'm left with one line in my persistence.xml:

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

Does anyone know a way to eliminate persistence.xml completely and move the persistence unit definition into the Spring configuration file? I tried Googling of course but couldn't find an answer.

Thanks!

1
You could consider replacing the JPA EntityManager with Hibernate's native SessionFactory.... less config that way - skaffman
@skaffman @duffymo ts ts ts, replacing an open standard - Sean Patrick Floyd
@S.P.Floyd: Yeah, but it's a sub-standard standard - skaffman

1 Answers

2
votes

persistence.xml is not mandatory for JPA configurations. You can create your entity manager with the EntityManagerFactory.createEntityManager(map) method. But LocalContainerEntityManagerFactoryBean doesn't have methods that helps creation of entity manager with a map. So you have to extend LocalContainerEntityManagerFactoryBean and override some methods according to your needs.