- import java.util.ArrayList;
- import java.util.List;
- import javax.persistence.Entity;
- import javax.persistence.FetchType;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Transient;
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- @Entity
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- public class MerType {
- private int id;
-
- private String name;
-
- private MerType parent;
-
- private List<MerType> children;
-
- public void addChild(MerType c){
- if(children == null){
- children = new ArrayList<MerType>();
- }
- children.add(c);
- }
-
- @Id
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- @ManyToOne(fetch=FetchType.EAGER, targetEntity=MerType.class)
- @JoinColumn(name="P_ID")
- public MerType getParent() {
- return parent;
- }
- public void setParent(MerType parent) {
- this.parent = parent;
- //設定父節點的時候,將當前節點加入到父節點的孩子列表中
- if(parent != null){
- parent.addChild(this);
- }
- }
- @Transient
- public List<MerType> getChildren() {
- return children;
- }
-
- public void setChildren(List<MerType> children) {
- this.children = children;
- }
- }
import java.util.ArrayList;import java.util.List;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.Transient;import org.hibernate.annotations.Cache;import org.hibernate.annotations.CacheConcurrencyStrategy;@Entity@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)public class MerType { private int id; private String name; private MerType parent; private List<MerType> children; public void addChild(MerType c){ if(children == null){ children = new ArrayList<MerType>(); } children.add(c); } @Id public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne(fetch=FetchType.EAGER, targetEntity=MerType.class) @JoinColumn(name="P_ID") public MerType getParent() { return parent; } public void setParent(MerType parent) { this.parent = parent; //設定父節點的時候,將當前節點加入到父節點的孩子列表中 if(parent != null){ parent.addChild(this); } } @Transient public List<MerType> getChildren() { return children; } public void setChildren(List<MerType> children) { this.children = children; }}
資料庫指令碼
CREATE TABLE MERTYPE (
ID int(11) NOT NULL PRIMARY KEY,
NAME varchar(20) NOT NULL,
P_ID int(11) default NULL)
測試代碼:
- SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
- Session session = sessionFactory.openSession();
-
- //一次取出全部節點
- Query query = session.createQuery("from MerType as m");
- List<MerType> list = query.list();
- //從一級緩衝中取出根節點(根節點為1000)
- MerType root = (MerType) session.get(MerType.class, 1000);
-
- session.close();
- sessionFactory.close();