英文:
I get an "Invalid persistence.xml" with createEntityManagerFactory but not @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 : 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>(这个 Maven 模块中唯一的实体类)</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" />
<!-- 其他属性... -->
</properties>
</persistence-unit>
</persistence>
我不明白为什么会发生这种情况,因为我的类位于同一个包中,同一个 Maven 模块中,使用相同的 Hibernate 实体和相同的 persistence.xml。
注: 将此类设置为 EJB 不是一个选项。
英文:
I have a project where we use Hibernate-jpa-2.1 and get an EntityManager istance both in this way
@PersistenceContext(unitName = "common-api")
EntityManager em;
and this
em = Persistence.createEntityManagerFactory("common-api").createEntityManager();
because some classes aren't EJB and others are (I guess). The problem is that the createEntityManagerFactory fails with the error:
Caused by: javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML : 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
This is the 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>
I don't understand why this happens because my classes are in the same package, same maven module, use the same Hibernate entities and the same persistence.xml.
N.B.: Making this class an EJB isn't an option.
答案1
得分: 2
这是一个错误,只有在元素顺序错误时才会出现(与您在此处发布的顺序不同)。在验证期间,persistence.xml
中元素的顺序很重要。它在 xsd 中有描述。在每个 persistence-unit
中,顺序如下:
- description
- provider
- jta-data-source
- non-jta-data-source
- mapping-file
- jar-file
- class
- exclude-unlisted-classes
- shared-cache-mode
- validation-mode
- properties
英文:
This is an error you should only get if the element order is wrong (and not the same as you posted here). The order of elements in persistence.xml
is important during validation. It's described in the xsd. In each persistence-unit
it is as follows:
- description
- provider
- jta-data-source
- non-jta-data-source
- mapping-file
- jar-file
- class
- exclude-unlisted-classes
- shared-cache-mode
- validation-mode
- properties
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论