Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
202 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...