Hibernate3:自引用对象

发布于 2024-08-13 08:02:01 字数 1830 浏览 7 评论 0原文

需要一些帮助来了解如何执行此操作;我将在文件系统上运行递归“查找”,并且希望将信息保留在单个数据库表中 - 具有自引用层次结构:

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

DirObject 表:

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

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

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

对于“根”目录,将使用parentid=0;真实的 ids 将从 1 开始,理想情况下我希望 hibernate 自动生成 ids。

有人可以为此提供建议的映射文件吗?作为第二个问题,我考虑这样做 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);

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

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

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

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

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.

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>

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

—━☆沉默づ 2024-08-20 08:02:01

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

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

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>

parent from child

<many-to-one name="parent" class="DirObject">
            <column name="parentid" />
 </many-to-one>
猫腻 2024-08-20 08:02:01

我相信这会起作用......完全未经测试。

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

I believe this will work ... completely untested.

<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>
坐在坟头思考人生 2024-08-20 08:02:01

您实际上可以拥有以下 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>

You can actually have the following Java entity:

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

And map it on the DIROBJECT table:

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

Using the following mapping:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文