簡體   English   中英

Hibernate3:自引用對象

[英]Hibernate3: Self-Referencing Objects

需要一些幫助來理解如何做到這一點; 我將在文件系統上運行遞歸“查找”,並且我希望將信息保存在單個數據庫表中 - 具有自引用的層次結構:

這是我要填充的數據庫表結構。

目錄表:

id       int NOT NULL,
name     varchar(255) NOT NULL,
parentid int NOT NULL);

這是我想要映射的Java類(僅顯示字段):

public DirObject {
    int id;
    String name;
    DirObject parent;
...

對於'root'目錄,將使用parentid = 0; real id將從1開始,理想情況下我希望hibernate自動生成id。

有人可以為此提供建議的映射文件; 作為第二個問題,我考慮過像這樣做Java類:

public DirObject {
    int id;
    String name;
    List<DirObject> subdirs;

我可以對這兩種方法中的任何一種使用相同的數據模型嗎? (當然使用不同的映射文件)。

---更新:所以我嘗試了下面建議的映射文件(謝謝!),在此重復以供參考:

<hibernate-mapping>
    <class name="my.proj.DirObject" table="category">
        ...   

        <set name="subDirs" lazy="true" inverse="true">
            <key column="parentId"/>
            <one-to-many class="my.proj.DirObject"/>
        </set>

        <many-to-one name="parent"
                     class="my.proj.DirObject"
                     column="parentId" cascade="all" />
    </class>

...並且改變了我的Java類以使'parentid'和'getSubDirs'[返回'HashSet']。

這似乎有用 - 謝謝,但這是我用來驅動它的測試代碼 - 我想我在這里沒做的事情,因為我認為Hibernate將負責保存Set中的從屬對象,而不必我做這明確嗎?

DirObject dirobject=new DirObject();
   dirobject.setName("/files");
   dirobject.setParent(dirobject);

   DirObject d1, d2;
   d1=new DirObject(); d1.setName("subdir1"); d1.setParent(dirobject);
   d2=new DirObject(); d2.setName("subdir2"); d2.setParent(dirobject);
   HashSet<DirObject> subdirs=new HashSet<DirObject>();
   subdirs.add(d1);
   subdirs.add(d2);
   dirobject.setSubdirs(subdirs);


   session.save(dirobject);
   session.save(d1);
   session.save(d2);

你可以從父母那里得到孩子

<set name="subdirs" lazy="false" cascade="all-delete-orphan" inverse="true">
            <key column="parentid " />
            <one-to-many class="DirObject" />
 </set>

孩子的父母

<many-to-one name="parent" class="DirObject">
            <column name="parentid" />
 </many-to-one>

我相信這會奏效......完全沒有經過考驗。

<hibernate-mapping>
    <class name="my.proj.DirObject" table="category">
        ...   

        <set name="subDirs" lazy="true" inverse="true">
            <key column="parentId"/>
            <one-to-many class="my.proj.DirObject"/>
        </set>

        <many-to-one name="parent"
                     class="my.proj.DirObject"
                     column="parentId" cascade="all" />
    </class>
</hibernate-mapping>

您實際上可以擁有以下Java實體:

public DirObject {
    int id;
    String name;
    DirObject parent;
    List<DirObject> subdirs;
    ...
}

並將其映射到DIROBJECT表:

ID       int NOT NULL,
NAME     varchar(255) NOT NULL,
PARENTID int NOT NULL);

使用以下映射:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="mypackage">

  <class name="DirObject" table="DIROBJECT">

    <id name="id" type="int">
      <column name="ID" />
      <generator class="native" />
    </id>

    <property name="name" type="string">
      <column name="NAME" length="150" not-null="true" unique="false" index="NAME" />
    </property>

    <bag name="subdirs" lazy="false" cascade="all-delete-orphan" inverse="true">
      <key column="PARENTID" />
      <one-to-many class="DirObject" />
    </bag>

    <many-to-one name="parent" class="DirObject">
      <column name="PARENTID" />
    </many-to-one>
  </class>

</hibernate-mapping>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM