使用 Spring 和 Hibernate 时是否需要persistence.xml? [英] Is persistence.xml requied when working with Spring and Hibernate?

查看:33
本文介绍了使用 Spring 和 Hibernate 时是否需要persistence.xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个带有 Spring JPA 和 Hibernate 的项目.persistence.xml 中的大部分内容都可以在 Spring applicationContext.xml 文件中指定.

那么 persistence.xml 是否不再需要了?

谢谢.

解决方案

更新: Spring 3.1 将支持 persistence.xml-free JPA 配置,参见 Spring 3.1 M2:配置增强.


darioo 的回答有利于实际使用,但在技术上并不正确.

PersistenceProvider 有两个工厂方法:

  • EntityManagerFactory createEntityManagerFactory(String emName, Map map) - 对于独立环境,persistence.xml 将由持久性提供程序解析.

  • EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) - 对于应用服务器环境,persistence.xml 由应用服务器解析,其内容作为 <代码>PersistenceUnitInfo.

Spring 的 LocalContainerEntityManagerFactoryBean 模拟应用服务器环境.因此,它自己解析 persistence.xml,将其内容与来自应用程序上下文的值合并,并使用第二个工厂方法将其传递给持久性提供者.

但是,获取persistence.xml数据的过程是可配置的:

  • 您可以使用 persistenceXmlLocation 属性配置 persistence.xml 文件的名称 - 这有助于避免与应用服务器的默认 JPA 初始化策略发生冲突.

  • 您可以通过设置自定义的 PersistenceUnitManager 策略来完全覆盖 PersistenceUnitInfo 的来源.

因此,实际上您可以通过编写自定义的 PersistenceUnitManager 在没有 persistence.xml 的情况下在 Spring 中配置 JPA,尽管这样的管理器不是开箱即用的.>

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.

解决方案

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.

这篇关于使用 Spring 和 Hibernate 时是否需要persistence.xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆