詳解Hibernate樹(shù)形結(jié)構(gòu)
本文向大家介紹Hibernate樹(shù)形結(jié)構(gòu),可能好多人還不了解Hibernate樹(shù)形結(jié)構(gòu),沒(méi)有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
在系統(tǒng)中,經(jīng)常會(huì)用到無(wú)限級(jí)的Hibernate樹(shù)形結(jié)構(gòu)分類(lèi),如組織機(jī)構(gòu)管理、商品/地區(qū)分類(lèi)等等。一般無(wú)外采用兩種方式:
◆一是類(lèi)似struts-menu的XML文件管理方式,配置起來(lái)比較方便,但很難與系統(tǒng)中其它應(yīng)用數(shù)據(jù)集成;
◆二是使用數(shù)據(jù)庫(kù)存儲(chǔ),定義父子關(guān)系。
在我們現(xiàn)在開(kāi)發(fā)的一個(gè)產(chǎn)品中,實(shí)現(xiàn)了一套Hibernate樹(shù)形結(jié)構(gòu)的處理方法,簡(jiǎn)介如下:
一.Hibernate樹(shù)形結(jié)構(gòu)顯示
使用的是xtree。為便于編輯維護(hù),自己寫(xiě)了一個(gè)左鍵彈出菜單(xtree的右鍵事件無(wú)法更改),進(jìn)行節(jié)點(diǎn)的添加、修改、刪除、轉(zhuǎn)移操作。(PS:這套維護(hù)界面是完全跨瀏覽器的,有興趣的不妨一試)
二.關(guān)聯(lián)關(guān)系:
可以使用objects對(duì)象來(lái)配置關(guān)聯(lián)關(guān)系,實(shí)現(xiàn)多對(duì)多/一對(duì)多等關(guān)系。在BaseTree中,getObjects()方法是abstract的,可以根據(jù)需要自己定義。如論壇分類(lèi)與每個(gè)分類(lèi)所對(duì)應(yīng)的貼子相關(guān)聯(lián),商品分類(lèi)與商品編碼相關(guān)聯(lián)等,可以根據(jù)需要來(lái)處理hbm文件。若需要多項(xiàng)關(guān)聯(lián),亦可擴(kuò)展。如菜單與用戶、部門(mén)、崗位分別進(jìn)行關(guān)聯(lián)
三.主要代碼:
- package test.testtree.base;
- import java.util.*;
- public abstract class BaseTree extends BasePojo implements Tree{
- protected String code;
- protected String name;
- protected String description;
- protected BaseTree parent;
- protected Set children = new HashSet();
- protected Set objects = new HashSet();
- public void setCode(String code) {
- this.code = code;
- }
- abstract public String getCode();
- public void setName(String name) {
- this.name = name;
- }
- abstract public String getName();
- public void setDescription(String description) {
- this.description = description;
- }
- abstract public String getDescription();
- abstract public Tree getParent();
- public boolean isRoot() {
- return (getParent()==null);
- }
- public boolean isLeaf() {
- return (this.getChildren().size()==0);
- }
- public boolean isParentOf(Tree tree) {
- if (tree==null || ((BaseTree) tree).equals(this)) {
- /*如果對(duì)方為空*/
- return false;
- }else if(this.isLeaf()){
- /*如果自己為葉子,則返回FALSE*/
- return false;
- }else if(tree.isRoot()){
- /*如果對(duì)方為根,返回FALSE*/
- return false;
- }else{
- BaseTree bt = (BaseTree) (tree.getParent());
- if (this.equals(bt)){
- /*如果對(duì)方的父節(jié)點(diǎn)是自己,則返回TRUE*/
- return true;
- }else{
- /*判斷對(duì)方的父節(jié)點(diǎn)是否是自己的孩子,進(jìn)行遞歸*/
- return isParentOf(bt);
- }
- }
- }
- public boolean isChildOf(Tree tree) {
- return (tree.isParentOf(this));
- }
- public void addChild(Tree tree) {
- children.add(tree);
- }
- public void rmChild(Tree tree) {
- children.remove(tree);
- ((BaseTree) tree).setParent(null);
- }
- public Set getAllLeaves() {
- Set set_old = this.getAllChildren();
- Set set = new HashSet();
- set.addAll(set_old);
- Iterator itr = set_old.iterator();
- while(itr.hasNext()){
- BaseTree bt = (BaseTree) itr.next();
- if (! bt.isLeaf()){
- set.remove(bt);
- }
- }
- return set;
- }
- public Set getAllChildren() {
- Set set = new HashSet();
- Stack stack = new Stack();
- stack.push(this);
- while(!stack.empty()){
- BaseTree bt = (BaseTree) stack.pop();
- set.add(bt);
- Iterator itr = bt.getChildren().iterator();
- while(itr.hasNext()){
- BaseTree btchild = (BaseTree) itr.next();
- stack.push(btchild);
- }
- }
- set.remove(this);
- return set;
- }
- public List getMeAndListAllChildren() {
- List lst = new Vector();
- lst.add(this);
- Iterator itr = this.getChildren().iterator();
- while(itr.hasNext()){
- BaseTree bt = (BaseTree) itr.next();
- lst.addAll(bt.getMeAndListAllChildren());
- }
- return lst;
- }
- abstract public Set getChildren();
- public void addObject(Object obj) {
- objects.add(obj);
- }
- public void rmObject(Object obj) {
- objects.remove(obj);
- }
- abstract public Set getObjects();
- public void setParent(Tree parent) {
- this.parent = (BaseTree) parent;
- }
- public void setChildren(Set children) {
- this.children = children;
- }
- public void setObjects(Set objects) {
- this.objects = objects;
- }
- }
【編輯推薦】