简体   繁体   English

通过XML在Hibernate中将类映射到带有子类的self

[英]Mapping classes to self with subclasses in Hibernate via XML

I have a table with parent_id values that reference itself. 我有一个表,其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 |
+----+------------+---------+-----------+------+---------+-----------+

When I try to map them like this: 当我尝试像这样映射它们时:

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

I get no errors but the Directory objects' attribute pobjects is empty. 我没有收到任何错误,但Directory对象的属性pobjects为空。 But in this case one of my root objects should be something like this: 但是在这种情况下,我的根对象之一应该是这样的:

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

But it actually is: 但实际上是:

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

So there's something probably wrong with the subclass mapping part in the XML annotations file. 因此,XML批注文件中的子类映射部分可能存在问题。 Is it possible at all to do what I am trying to achieve? 完全有可能做我想达到的目标吗?

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

Somehow got it working. 不知何故使它工作。 Might be because of this change (and also making the directories Pobject type in the respective class) or maybe the gods of Java are simply merciful today. 可能是由于此更改(以及使相应类中的目录Pobject类型),或者今天的Java之神简直是仁慈。

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

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