我得到了一个"Invalid persistence.xml" createEntityManagerFactory,但没有@PersistenceContext



我有一个项目,我们使用Hibernate-jpa-2.1,并以这种方式获得EntityManager距离

@PersistenceContext(unitName = "common-api")
EntityManager em;

和这个

em = Persistence.createEntityManagerFactory("common-api").createEntityManager();

因为有些类不是EJB,而另一些类(我想(是。问题是createEntityManagerFactory失败,并返回错误:

Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML [line : -1, column : -1] : cvc-complex-type.2.4.a: Invalid content was found starting with element 'class'. One of '{"http://xmlns.jcp.org/xml/ns/persistence":validation-mode, "http://xmlns.jcp.org/xml/ns/persistence":properties}' is expected.
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.validate(PersistenceXmlParser.java:357)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.loadUrl(PersistenceXmlParser.java:290)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.parsePersistenceXml(PersistenceXmlParser.java:94)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.doResolve(PersistenceXmlParser.java:84)
at org.hibernate.jpa.boot.internal.PersistenceXmlParser.locatePersistenceUnits(PersistenceXmlParser.java:66)
at org.hibernate.jpa.HibernatePersistenceProvider.getEntityManagerFactoryBuilderOrNull(HibernatePersistenceProvider.java:80)
... 80 more

这是persistence.xml:


<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="common-api" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>(...)</jta-data-source>
<class>(the only entity we have in this Maven module)</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.validation.mode" value="NONE" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL57InnoDBDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.max_fetch_depth" value="2" />
<property name="hibernate.use_sql_comments" value="true" />
<property name="hibernate.use_identifer_rollback" value="true" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.jdbc.batch_versioned_data" value="false" />
<property name="hibernate.jdbc.use_get_generated_keys" value="true" />
<property name="hibernate.hbm2ddl.auto" value="none"/>
</properties>
</persistence-unit>
</persistence>

我不明白为什么会发生这种情况,因为我的类在同一个包中,同一个maven模块中,使用相同的Hibernate实体和相同的persistence.xml.

注意:使此类成为EJB不是一种选择。

这是一个错误,只有在元素顺序错误(与此处发布的不同(时才会出现。persistence.xml中元素的顺序在验证过程中非常重要。xsd中对此进行了描述。在每个persistence-unit中,它如下:

  1. 描述
  2. 提供者
  3. jta数据源
  4. 非jta数据源
  5. 映射文件
  6. jar文件
  7. 排除未列出的类别
  8. 共享缓存模式
  9. 验证模式
  10. 特性

相关内容

  • 没有找到相关文章

最新更新