简体   繁体   中英

Mapping classes to self with subclasses in Hibernate via XML

I have a table with parent_id values that reference itself.

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

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