Hibernate3:自引用对象 [英] Hibernate3: Self-Referencing Objects

查看:162
本文介绍了Hibernate3:自引用对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些帮助才能理解如何做到这一点;我将在文件系统上运行递归查找,并且我希望将信息保存在单个数据库表中 - 具有自引用层次结构:

Need some help on understanding how to do this; I'm going to be running recursive 'find' on a file system and I want to keep the information in a single DB table - with a self-referencing hierarchial structure:

这是我要填充的数据库表结构。

This is my DB Table structure I want to populate.

DirObject表:

DirObject Table:

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

这是我要映射的拟议Java类(仅显示字段):

Here is the proposed Java Class I want to map (Fields only shown):

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

对于'root'目录,将使用parentid = 0; real id将从1开始,理想情况下我希望hibernate自动生成id。

For the 'root' directory was going to use parentid=0; real ids will start at 1, and ideally I want hibernate to autogenerate the ids.

有人可以为此提供建议的映射文件;作为第二个问题,我考虑过像这样做Java类:

Can somebody provide a suggested mapping file for this please; as a secondary question I thought about doing the Java Class like this instead:

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

我可以对这两种方法中的任何一种使用相同的数据模型吗? (当然使用不同的映射文件)。

Could I use the same data model for either of these two methods ? (With a different mapping file of course).

---更新:所以我尝试了下面建议的映射文件(谢谢!),在此重复以供参考:

--- UPDATE: so I tried the mapping file suggested below (thanks!), repeated here for reference:

<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']。

...and altered my Java class to have BOTH 'parentid' and 'getSubDirs' [returning a 'HashSet'].

这似乎有效 - 谢谢,但这是我用来驱动它的测试代码 - 我想我在这里做的不对,因为我认为Hibernate将保留Set中的下级对象而不必显式地执行此操作?

This appears to work - thanks, but this is the test code I used to drive this - I think I'm not doing something right here, because I thought Hibernate would take care of saving the subordinate objects in the Set without me having to do this explicitly ?

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);


推荐答案

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

you can get the children from parent

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

这篇关于Hibernate3:自引用对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆