java 使用 Spring 和 Hibernate 时是否需要persistence.xml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4736301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:47:41  来源:igfitidea点击:

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

javahibernatespringjpa

提问by user373201

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

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

So is the persistence.xmlrequired anymore?

那么是否需要persistence.xml了?

Thanks.

谢谢。

回答by axtavt

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

更新:Spring 3.1 将支持无persistence.xmlJPA 配置,请参阅Spring 3.1 M2:配置增强



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

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

PersistenceProviderhas two factory methods:

PersistenceProvider有两种工厂方法:

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

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

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

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

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

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

However, process of obtaining persistence.xmldata is configurable:

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

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

  • You can completely override the source of PersistenceUnitInfoby setting a custom PersistenceUnitManagerstrategy.

  • 您可以persistence.xml使用persistenceXmlLocationproperty配置文件的名称- 避免与应用程序服务器的默认 JPA 初始化策略冲突很有用。

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

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

因此,实际上您可以在 Spring 中配置 JPA,而无需persistence.xml编写自定义PersistenceUnitManager,尽管这样的管理器不是开箱即用的。

回答by darioo

persistence.xmlis needed when you're using Hibernate through JPA, even though you're using Spring JPA. If you're using Hibernate directly, then persistence.xmlisn't needed.

persistence.xml即使您使用的是 Spring JPA,当您通过 JPA 使用 Hibernate 时也需要。如果您直接使用 Hibernate,则persistence.xml不需要。

回答by Edwin Dalorzo

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.

JPA 规范没有说明需要该文件的任何地方,但在 EntityManagerFactory 和 EntityManager 的契约定义中,实际上在整个规范中,都反复提到了 persistence.xml 文件。

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.

我最近不得不处理是否可以在不使用 persistence.xml 的情况下以编程方式使用 Hibernate 3.6 配置 JPA 2.0 的问题。

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.

我从未使用过 spring,因此我不知道它是否使用任何技巧来克服这个问题。