1: The code has not been debugged, just wrote it!
Java code:
package com.yours.tree; import java.util.List; import java.util.Iterator; import org.apache.log4j.Logger; import net.sf.hibernate.Hibernate; import net.sf.hibernate.HibernateException; import net .sf.hibernate.query; import net.sf.hibernate.Session; import com.yours.datamodel.hibernate.hibernatesis; import com.yours.admin.jdo.auth;
/ ** *
Title: p> *
Description: p> *
Copyright: Copyright (c) 2004 p> *
Company: p> * @ Author Duan Hongjie * @version 1.0 * / public class tree {private static logger log = logger.getlogger (TreeAuth.class);
/ ** * Take the root leaf * @return itrator * / public iterator getroot () {iterator itrator = null; string sql = "from auth as auth where auth.parentid is null"; try {session session = HibernateSession.currentSession () Query Query = session.createQuery (SQL); itrator = query.iterate ();} catch (hibernateExcect e) {log.error (e);} return itrator;}
/ ** * stub leaf * @param Object Leaf * / public void setRoot (Auth auth) {try {Session session = HibernateSession.currentSession (); HibernateSession.currentTransaction (); session.save (auth); session.flush () ;} Catch (hibernateException e) {log.error (e);}}
/ * ** taken cotyledon * @param parentLeaf Leaf * @return Iterator * / public Iterator getChild (String parentid) {Iterator iterator = null; String sql = "from Auth as auth where auth.parentid = '" parentid "' "; Try {session session = hibernatesis (); query query = session.createQuery (SQL); item = query.Iterate ();} catch (hibernateException E) {log.error (e);} return itrator;} / ** * 取 子 * @Param Parentleaf Leaf * @return int * / public int getChildcount (String ParentID) {INT count = 0; string sql = "select count (*) from auth as auth where auth.parentID =
'" Parentid "' "; try {session session = hibernatesis (); count = ((Integer) session.Iterate (SQL) .Next ()). INTVALUE ();} catch (hibernateException E) {logG (E);} return count;
/ * ** stored cotyledon * @param parentLeaf Leaf * / public void setChild (Auth auth, String parentid) {auth.setParentid (parentid); try {Session session = HibernateSession.currentSession (); HibernateSession.currentTransaction (); session. Save (auth); session.flush ();} catch (hibernateException e) {log.error (e);}}
/ ** * * remove the leaves and the cotyledons it * @param leaf Leaf * / public void deleteBranch (Auth auth) {try {Session session = HibernateSession.currentSession (); HibernateSession.currentTransaction (); String id = auth.getId (); Iterator Iterator = getChild (ID); if (item! = Null) {while (iterator.hasnext ()) {auth auth1 = (auth) item.next (); deletebranch (auth1);}} session.delete (auth); session.flush ();} catch (hibernateException e) {log.error (e);}}} 2:
I don't understand, HB still wants this method? I can use the Composite model .. Direct use of relationships (HB is very in place for the relationship between the objects). The following code has not been tested, just use TXT tools written.
Java code:
/ ** * @ hibernate.class * table = "tree" * / public class Component {private long id; private String name; private Component parent; private Set children = new HashSet (); private Component () {} public Component ( String name) {this.name = name;}
/ ** * @ hibernate.id * generator-class = "native" * / public getId () {return id;} private setId (long id) {this.id = id;}
/ ** * @ hibernate.property * length = "64" * not-null = "true" * / public string getName () {return name;} public void setname (String name) {this.name = name;}
/ ** * Get parent nodes * @ hibernate.many-to-one * column = "parentID" * / public component getParent () {return parent;} public void setparent (component parent) {this.parent = parent;} / ** * Get sub-node * @ hibernate.set * lazy = "true" * table = "tre" * @ hibernate.collection-key * column = "parentID" * @ hibernate.collection-one-to-man * class = "Component" * / public Set getChildren () {return children;} private void setChildren (Set children) {this.children = children;} public void addChild (Component child) {children.add (child);} public void removeChild ( Component child) {childrent.remove (child);} public void clearchildren () {children = new hashset () }}
3:
Although it is not familiar with Hibernate, it has been used several JTREEs, and there are several ways upstairs. I think some grass rate is probably written. In addition, since Children uses hashset, whether the Component class should also consider implementing the equals method and the HashCode method?
Java code:
public void addChild (Component child) {children.add (child); child.setParent (this);} public boolean removeChild (Component child) {return children.remove (child);} public void clearChildren () {children.clear ( }
4:
Upstairs is right (but I don't think there is not much impact, unless the object is used to make Map "Key, you must use Equals, Hashcode, because the average will not put the same record in the same set, of course each class It is best to achieve equals, havehcode, and torstring is the best). In fact, I don't do it above, because it is a variant with the landlord method, so it will still transmit SQL to the database each time, take Children or other), efficiency The problem is not the best. After reading the source code of Hashset, it was found to be managed with a HashMap, using the object of add as Key. However, because Key and Value did not have a lot of relationships, I think I can do not implement equals and havehcode. But It is also achieved .5: Using Composite mode, it is also a good method. However, I think my method will have better performance, such as take a child, my method is a layer of one layer, there is more Good flexibility and can reduce memory. Of course, some people are advised to use Composite model directly. It is more OOP. But I prefer my way!