【问题标题】:Mapping classes to self with subclasses in Hibernate via XML通过 XML 在 Hibernate 中使用子类将类映射到 self
【发布时间】:2016-07-07 14:28:21
【问题描述】:

我有一个带有 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 注释文件中的子类映射部分可能有问题。有没有可能做我想要实现的目标?

【问题讨论】:

    标签: java xml hibernate


    【解决方案1】:
    <many-to-one name="directory" class="com.naples.file.Pobject" fetch="select">
        <column name="parent_id"/>
    </many-to-one>
    

    不知何故让它工作了。可能是因为这种变化(并且还使目录Pobject 在相应的类中输入)或者Java 之神今天只是仁慈的。

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      相关资源
      最近更新 更多