本文介绍了用hibernate xml配置文件进行树建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个叫区域的课程。每个区域可以有多个子区域(属于同一类型)。



我需要能够确定子区域和根区域。子区域可以有自己的子区域。



这可能会有几个级别。



示例:

  Rootarea 
| - 分区1
| | - 分区2
| |
| | - 分区3
|
| - 分区4
| | - 分区5

公共类区域{

私人长ID;

私人地区父母;
私人列表<区域>儿童;

私人字符串名称;

// getters and setters here ommitted
}

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



和...



我加载到内存的区域树?



谢谢

解决方案

<$ p $ ()
{
public boolean isRootArea()
{
return getParent()== null;
}
}


< id name =Id>
< generator class =.../>
< / id>

< bag name =childrencascade =all-delete-orphan>
< key column =parent_id/>
<一对多等级=区域/>
< / bag>
<多对一课程=区域名称=父母>
< column name =parent_id/>
< /多对一>
< / class>

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


I've got a class called "Area". Each Area can have multiple sub-areas (of the same type).

I need to be able to determine sub-areas and root-areas. Sub-areas can have sub-areas themself.

This can be quite a few levels deep.

Example:

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
}

How do I model such a Java-class with Hibernate xml config files?

and ...

How do I load to memory the areas tree?

thanks

解决方案
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();

这篇关于用hibernate xml配置文件进行树建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-22 00:42