简体   繁体   English

如果将歧视标签在休眠状态下的位置向下移动,则会发生异常

[英]Exception occurs if the position of discriminator tag in hibernate is moved down

I am new to Hibernate. 我是休眠的新手。 I am trying to map both my super-class and sub-class to a single table. 我试图将我的超类和子类都映射到一个表。

<class name="Employee" table="EmpWithManager">
        <id name="id" column="ID">
            <generator class="native"></generator>
        </id>
        <discriminator column="EMP_TYPE" type="string"></discriminator>
        <property name="firstName" column="FIRST_NAME"></property>
        <property name="lastName" column="LAST_NAME"></property>
        <property name="salary" column="SALARY"></property>


        <subclass name="Manager" extends="Employee">
            <property name="managerId" column="MAN_ID"></property>
            <property name="noOfEmployees" column="NUMBER_EMP"></property>
        </subclass>

    </class>

This works fine but if change the position of the discriminator tag as follows: 这可以正常工作,但是如果按以下方式更改discriminator标签的位置,则:

<class name="Employee" table="EmpWithManager">
    <id name="id" column="ID">
        <generator class="native"></generator>
    </id>

    <property name="firstName" column="FIRST_NAME"></property>
    <discriminator column="EMP_TYPE" type="string"></discriminator>
    <property name="lastName" column="LAST_NAME"></property>
    <property name="salary" column="SALARY"></property>


    <subclass name="Manager" extends="Employee">
        <property name="managerId" column="MAN_ID"></property>
        <property name="noOfEmployees" column="NUMBER_EMP"></property>
    </subclass>

</class>

This re-ordering gives me the below exception: 这种重新排序给了我以下异常:

Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)".

Please anybody tell me why this is happening and whether the position of discriminator should be in the beginning? 请有人告诉我为什么会这样,以及鉴别器的位置是否应该在开始时?

According to the hibernate document type definitions (DTD) listed here , the position of the discriminator tag must be after the id tag. 根据此处列出的休眠文档类型定义(DTD) ,鉴别标记的位置必须在id标记之后。 Essentially the structure of the xml document in this situation is pre-defined, and you must follow the pre-defined format, and that is why you see an error after moving the discriminator tag. 本质上,在这种情况下xml文档的结构是预定义的,因此必须遵循预定义的格式,这就是为什么在移动discriminator标签后看到错误的原因。

From the JBoss docs: 5.1.8 - Discriminator : 从JBoss文档: 5.1.8- 鉴别器

The <discriminator> element is required for polymorphic persistence using the table-per-class-hierarchy mapping strategy and declares a discriminator column of the table. <discriminator>元素是使用逐表类映射策略进行多态持久性所必需的,并声明表的discriminator列。 The discriminator column contains marker values that tell the persistence layer what subclass to instantiate for a particular row. 鉴别符列包含标记值,这些值告诉持久性层要为特定行实例化的子类。 A restricted set of types may be used: string, character, integer, byte, short, boolean, yes_no, true_false. 可以使用一组受限制的类型:字符串,字符,整数,字节,短型,布尔型,yes_no,true_false。

I'd imagine that you must define how properties will be discriminated against before you define them and that is the reasoning for the structure within the DTD. 我以为您必须先定义如何区分属性,然后再定义属性,这就是DTD中结构的原因。

If you look at the http://hibernate.org/dtd/ entry for hibernate-mapping-3.0.dtd it defines the class element as follows. 如果查看hibernate-mapping-3.0.dtdhttp://hibernate.org/dtd/条目,它将定义class元素,如下所示。 Order is important as this is a DTD. 订单很重要,因为这是DTD。 Note that discriminator? 注意discriminator? comes after (id|composite-id) and before the long entry with property . (id|composite-id) ,在具有property的长条目之前。 This ordering requirement is not explicitly mentioned in the (current) hibernate documentation. (当前)休眠文档中未明确提及此排序要求。

<!ELEMENT class (
    meta*,
    subselect?,
    cache?,
    synchronize*,
    comment?,
    tuplizer*,
    (id|composite-id),
    discriminator?,
    natural-id?,
    (version|timestamp)?,
    (property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,
    ((join*,subclass*)|joined-subclass*|union-subclass*),
    loader?,sql-insert?,sql-update?,sql-delete?,
    filter*,
    fetch-profile*,
    resultset*,
    (query|sql-query)*
)>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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