简体   繁体   中英

JPA ORM.xml mapping file in <mapped-superclass> cannot put <basic> together <id > or <version >

I'm have many class with JPA Annotations and I need to change it to orm mappingfile.

But, there is a stranger error in the XML

My 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>

if has < id> tag or < version> tag together < basic> show the error :

 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.

What's wrong? Can I define atribute in the superclass in .xml as defined in the model class with annotation? T

he class with annotation are works with no problem, is there any limmitation using .xml than annotation?

Element version should occur immediately after last occurrence of basic element, not before them as it is now.

In this case right order is:

    <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>

As a side note, column updated_at seems to be mapped twice.

This order of elements under attributes element is defined in 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM