I get an "Invalid persistence.xml" with createEntityManagerFactory but not @PersistenceContext

huangapple go评论79阅读模式
英文:

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 = &quot;common-api&quot;)
EntityManager em;

and this

    em = Persistence.createEntityManagerFactory(&quot;common-api&quot;).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 &#39;class&#39;. One of &#39;{&quot;http://xmlns.jcp.org/xml/ns/persistence&quot;:validation-mode, &quot;http://xmlns.jcp.org/xml/ns/persistence&quot;:properties}&#39; 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:


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

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 中,顺序如下:

  1. description
  2. provider
  3. jta-data-source
  4. non-jta-data-source
  5. mapping-file
  6. jar-file
  7. class
  8. exclude-unlisted-classes
  9. shared-cache-mode
  10. validation-mode
  11. 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:

  1. description
  2. provider
  3. jta-data-source
  4. non-jta-data-source
  5. mapping-file
  6. jar-file
  7. class
  8. exclude-unlisted-classes
  9. shared-cache-mode
  10. validation-mode
  11. properties

huangapple
  • 本文由 发表于 2020年10月23日 21:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64501047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定