有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA ORM。<mappedsuperclass>中的xml映射文件不能将<basic>放在一起<id>或<version>

我有很多带有JPA注释的类,我需要将其更改为orm mappingfile

但是,XML中有一个奇怪的错误

我的XML

<?xml version="1.0" encoding="UTF-8"?>
 <entity-mappings version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm 
                    http://xmlns.jcp.org/xml/ns/persistence/orm_2_1.xsd">


<mapped-superclass class="model.base.BaseModel">
    <entity-listeners>
        <entity-listener class="model.base.BaseModelListener"></entity-listener>
    </entity-listeners>

    <attributes>


        <id name="id">
            <generated-value strategy="IDENTITY" />
        </id>

        <version name="version"></version>

        <basic name="createdAt" optional="false">
            <column name="updated_at" nullable="false" />
            <temporal>TIMESTAMP</temporal>
        </basic>

        <basic name="updatedAt" optional="false">
            <column name="updated_at" nullable="false" />
            <temporal>TIMESTAMP</temporal>
        </basic>

    </attributes>

</mapped-superclass>


<entity class="model.general.Account">
    <attributes>            
        <basic name="email">
        </basic>
    </attributes>
</entity>

如果有<;id>;标记或<;版本>;一起标记<;基础>;显示错误:

 cvc-complex-type.2.4.a: Invalid content was found starting with element 'basic'. One of              {"xmlns.jcp.org/xml/ns/persistence/orm":version "http://xmlns.jcp.org/xml/ns/persistence/orm":many- to-one, http://xmlns.jcp.org/xml/ns/persistence/orm":one-to-many, "    http://xmlns.jcp.org/xml/ns/
         persistence/orm":one-to-one, "http://xmlns.jcp.org/xml/ns/persistence/orm":many-to-many,  "http:// xmlns.jcp.org/xml/ns/persistence/orm":element-collection,    "http://xmlns.jcp.org/xml/ns/persistence/ orm":embedded, http://xmlns.jcp.org/xml/ns/persistence/orm":transient}' is expected.

怎么了? 我可以在中的超类中定义atribute吗。xml是否在模型类中定义并带有注释?T

带注释的类是没有问题的作品,是否有任何限制使用。xml而不是注释


共 (1) 个答案

  1. # 1 楼答案

    元素version应该在basic元素最后一次出现之后立即出现,而不是像现在这样出现在它们之前

    在这种情况下,正确的顺序是:

        <id name="id">
            <generated-value strategy="IDENTITY" />
        </id>
    
        <basic name="createdAt" optional="false">
            <column name="updated_at" nullable="false" />
            <temporal>TIMESTAMP</temporal>
        </basic>
    
        <basic name="updatedAt" optional="false">
            <column name="updated_at" nullable="false" />
            <temporal>TIMESTAMP</temporal>
        </basic>
    
        <version name="version"></version>
    

    作为补充说明,更新的_at列似乎映射了两次

    attributes元素下的元素顺序在schema中定义:

    <xsd:sequence>
      ....
      <xsd:choice>
        <xsd:element name="id" type="orm:id" 
                     minOccurs="0" maxOccurs="unbounded"/>
        <xsd:element name="embedded-id" type="orm:embedded-id" 
                     minOccurs="0"/>
      </xsd:choice>
      <xsd:element name="basic" type="orm:basic"
                   minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="version" type="orm:version"
                   minOccurs="0" maxOccurs="unbounded"/>
      <xsd:element name="many-to-one" type="orm:many-to-one"
                   minOccurs="0" maxOccurs="unbounded"/>
    ....
    </xsd:sequence>