Hibernate高效處理Tree型資料

來源:互聯網
上載者:User
  1. import java.util.ArrayList;   
  2. import java.util.List;   
  3. import javax.persistence.Entity;   
  4. import javax.persistence.FetchType;   
  5. import javax.persistence.Id;   
  6. import javax.persistence.JoinColumn;   
  7. import javax.persistence.ManyToOne;   
  8. import javax.persistence.Transient;   
  9. import org.hibernate.annotations.Cache;   
  10. import org.hibernate.annotations.CacheConcurrencyStrategy;   
  11. @Entity  
  12. @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)   
  13. public class MerType {   
  14.  private int id;   
  15.     
  16.  private String name;   
  17.     
  18.  private MerType parent;   
  19.     
  20.  private List<MerType> children;   
  21.     
  22.  public void addChild(MerType c){   
  23.   if(children == null){   
  24.    children = new ArrayList<MerType>();   
  25.   }   
  26.   children.add(c);   
  27.  }   
  28.     
  29.  @Id  
  30.  public int getId() {   
  31.   return id;   
  32.  }   
  33.  public void setId(int id) {   
  34.   this.id = id;   
  35.  }   
  36.  public String getName() {   
  37.   return name;   
  38.  }   
  39.  public void setName(String name) {   
  40.   this.name = name;   
  41.  }   
  42.     
  43.  @ManyToOne(fetch=FetchType.EAGER, targetEntity=MerType.class)   
  44.  @JoinColumn(name="P_ID")   
  45.  public MerType getParent() {   
  46.   return parent;   
  47.  }   
  48.  public void setParent(MerType parent) {   
  49.   this.parent = parent;   
  50.   //設定父節點的時候,將當前節點加入到父節點的孩子列表中   
  51.   if(parent != null){   
  52.     parent.addChild(this);   
  53.   }   
  54.  }   
  55.  @Transient  
  56.  public List<MerType> getChildren() {   
  57.   return children;   
  58.  }   
  59.     
  60.  public void setChildren(List<MerType> children) {   
  61.   this.children = children;   
  62.  }   
  63. }  
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)

測試代碼:

Java代碼
  1. SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();   
  2. Session session = sessionFactory.openSession();   
  3.   
  4. //一次取出全部節點   
  5. Query query = session.createQuery("from MerType as m");   
  6. List<MerType> list = query.list();   
  7. //從一級緩衝中取出根節點(根節點為1000)   
  8. MerType root = (MerType) session.get(MerType.class, 1000);   
  9.   
  10. session.close();   
  11. sessionFactory.close();  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.