hibernate--树状映射(至关重要)

来源:互联网 发布:qq群秒加僵尸软件 编辑:程序博客网 时间:2024/06/07 02:28

一张表即可完成映射:

Org.java:

package com.bean;import java.util.HashSet;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.ManyToOne;import javax.persistence.OneToMany;@Entitypublic class Org {private int id;private String name;private Set<Org> children = new HashSet<Org>();private Org parent;@Id@GeneratedValuepublic 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;}@OneToMany(mappedBy="parent",cascade=CascadeType.ALL,fetch=FetchType.EAGER)public Set<Org> getChildren() {return children;}public void setChildren(Set<Org> children) {this.children = children;}@ManyToOne(cascade=CascadeType.ALL)@JoinColumn(name="parent_id")public Org getParent() {return parent;}public void setParent(Org parent) {this.parent = parent;}}
hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration>   <session-factory>     <property name="connection.driver_class">com.mysql.jdbc.Driver</property>         <property name="connection.url">jdbc:mysql://localhost:3306/user</property>          <property name="connection.username">root</property>        <property name="connection.password">root</property>          <property name="dialect">org.hibernate.dialect.MySQLDialect</property>     <property name="show_sql">true</property>          <property name="hbm2ddl.auto">update</property>     <property name="show_sql">true</property>     <property name="format_sql">true</property>            <property name="current_session_context_class">thread</property>      <mapping class="com.bean.Org"/>   </session-factory></hibernate-configuration>        
测试类:

package com.test;import java.awt.print.Printable;import java.util.Set;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import com.bean.Org;public class TestOrg {   public static void main(String []args) {     // testSave(); testLoad();  }      public static void testSave() {Configuration cfg = new Configuration();cfg.configure();SessionFactory sessionFactory = cfg.buildSessionFactory();Session session = sessionFactory.getCurrentSession();Org o = new Org();o.setName("总公司");    Org o1 = new Org();    o1.setName("分公司1");    Org o2 = new Org();    o2.setName("分公司2");    Org o11 = new Org();    o11.setName("分公司1下的部门1");    Org o12 = new Org();    o12.setName("分公司1下的部门2");        o12.setParent(o1);    o11.setParent(o1);    o1.setParent(o);    o2.setParent(o);        o1.getChildren().add(o12);    o1.getChildren().add(o11);    o.getChildren().add(o2);    o.getChildren().add(o1);session.beginTransaction();session.save(o);session.getTransaction();}      public static void testLoad() {Configuration cfg = new Configuration();cfg.configure();SessionFactory sessionFactory = cfg.buildSessionFactory();Session session = sessionFactory.getCurrentSession();session.beginTransaction();        Org o = (Org)session.load(Org.class, 1);        print(o,0);session.getTransaction();}private static  void print(Org o,int level) {//递归打印String preString = "";for (int i = 0; i < level; i++) {preString+="----";}System.out.println(preString+o.getName());for (Org org : o.getChildren()) {print(org,level+1);}}   }

save后,数据库的内容:


load:


数据库表的设计原理: