通过XML在Hibernate中将类映射到self以及子类

时间:2016-07-07 14:28:21

标签: java xml hibernate

我有一个parent_id值的表格引用自己。

+----+------------+---------+-----------+------+---------+-----------+
| id | title      | user_id | published | uri  | type_id | parent_id |
+----+------------+---------+-----------+------+---------+-----------+
|  1 | file1.bpmn |       1 |         0 | NULL |       1 |         5 |
|  2 | file2.bpmn |       1 |         0 | NULL |       1 |         5 |
|  3 | file3.bpmn |       1 |         0 | NULL |       1 |         5 |
|  4 | file4.bpmn |       2 |         0 | NULL |       1 |         6 |
|  5 | root       |       1 |         0 | NULL |       2 |      NULL |
|  6 | root       |       2 |         0 | NULL |       2 |      NULL |
|  7 | root       |       3 |         0 | NULL |       2 |      NULL |
|  8 | SomeFolder |       1 |         0 | NULL |       2 |         5 |
+----+------------+---------+-----------+------+---------+-----------+

当我尝试像这样映射它们时:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping">
<hibernate-mapping>
    <class name="com.naples.file.Pobject" table="pobjects" catalog="pleak" discriminator-value="-1">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>

        <discriminator column="type_id" type="java.lang.Integer"/>

        <many-to-one name="user" class="com.naples.user.User" fetch="select">
            <column name="user_id" not-null="true" />
        </many-to-one>

        <many-to-one name="directory" class="com.naples.file.Directory" fetch="select">
            <column name="parent_id"/>
        </many-to-one>

        <property name="title" type="string">
            <column name="title" length="255" not-null="true" />
        </property>

        <set name="permissions" table="permissions" inverse="true" lazy="false" fetch="select" cascade="all">
          <key>
            <column name="pobject_id" not-null="true"/>
          </key>
          <one-to-many class="com.naples.file.Permission"/>
        </set>

        <subclass name="com.naples.file.File" extends="Pobject" discriminator-value="1">
            <property name="published" type="boolean">
                <column name="published" length="255" not-null="true" />
            </property>
            <property name="uri" type="string">
                <column name="uri" length="255" />
            </property>
        </subclass>

        <subclass name="com.naples.file.Directory" extends="Pobject" discriminator-value="2">
            <set name="pobjects" table="pobjects" inverse="false" lazy="false" fetch="select">
                <key>
                    <column name="parent_id" not-null="true"/>
                </key>
                <one-to-many class="com.naples.file.Pobject"/>
            </set>
        </subclass>

        <filter name="userFilter" condition="user_id = :userFilterParam"/>
    </class>

    <filter-def name="userFilter">
        <filter-param name="userFilterParam" type="java.lang.Integer"/>
    </filter-def>
</hibernate-mapping>

我没有错误,但Directory对象'属性pobjects为空。但在这种情况下,我的一个根对象应该是这样的:

id = 5
title = "root"
user = UserObjectWithID1
directory = null
permissions = [PermissionObject1, PermissionObject2, ...]
pobjects = [FileObjectWithID1, FileObjectWithID2, FileObjectWithID3, FolderObjectWithID8]

但实际上是:

id = 5
title = "root"
user = UserObjectWithID1
directory = null
permissions = [PermissionObject1, PermissionObject2, ...]
pobjects = []

因此,XML注释文件中的子类映射部分可能存在问题。是否有可能做我想要实现的目标?

1 个答案:

答案 0 :(得分:0)

<many-to-one name="directory" class="com.naples.file.Pobject" fetch="select">
    <column name="parent_id"/>
</many-to-one>

不知何故让它发挥作用。可能是因为这种变化(并且还在相应的类中创建了目录Pobject)或者今天Java的神灵只是仁慈的。