【问题标题】:Tree modeling with hibernate xml config files使用休眠 xml 配置文件进行树建模
【发布时间】:2012-03-14 12:52:08
【问题描述】:

我有一个名为“Area”的课程。每个区域可以有多个子区域(相同类型)。

我需要能够确定子区域和根区域。子区域本身可以有子区域。

这可能有好几个层次。

例子:

Rootarea  
|- Subarea 1  
|   |- Subarea 2  
|   |
|   |- Subarea 3
|
|- Subarea 4
|   |- Subarea 5

public class Area {

   private long id;

   private Area parent; 
   private List<Area> children;

   private String name;

   //getters and setters here ommitted
}

如何使用 Hibernate xml 配置文件为这样的 Java 类建模?

和...

如何将区域树加载到内存中?

谢谢

【问题讨论】:

  • 您打算如何将其存储在数据库中?都在同一张桌子上?

标签: hibernate tree


【解决方案1】:
class Area
{
    public Boolean isRootArea()
    {
        return getParent() == null;
    }
}


  <class name="Area" table="`AREA`">
    <id name="Id">
      <generator class="..." />
    </id>

    <bag name="children" cascade="all-delete-orphan">
      <key column="parent_id" />
      <one-to-many class="Area" />
    </bag>
    <many-to-one class="Area" name="parent">
      <column name="parent_id" />
    </many-to-one>
  </class>


var rootareas = session.createCriteria(Area.class).add(Restriction.IsNull("parent")).setFetchMode("children", FetchMode.eager).list();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-17
    • 2014-07-27
    • 2016-05-06
    • 2010-12-26
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多