hiberante 建立树形数据存储

package com.edu.hpu;

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;
import javax.persistence.Table;

@Entity
@Table(name="_org")
public class Organization {

    private int id;
    private String name;
    private Organization parent;
    private Set<Organization> children = new HashSet<Organization>();
   
    @Id
    @GeneratedValue
    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
    @JoinColumn(name="parentID")
    public Organization getParent() {
        return parent;
    }
    public void setParent(Organization parent) {
        this.parent = parent;
    }
   
    @OneToMany(mappedBy="parent" ,  cascade={CascadeType.ALL} , fetch=FetchType.EAGER)
    public Set<Organization> getChildren() {
        return children;
    }
    public void setChildren(Set<Organization> children) {
        this.children = children;
    }
   
}


package com.edu.hpu;

import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestTree {
   
    private static SessionFactory sf = null;
   
    @BeforeClass
    public static void beforeClass() {
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();       
        sf = configuration.buildSessionFactory(serviceRegistry);
    }
   
    @AfterClass
    public static void afterClass() {
        sf.close();
    }

    @Test
    public void testExport() {
        new SchemaExport(new Configuration().configure()).create(true , true);
    }
   
    @Test
    public void testSave() {
        Organization o = new Organization();
        o.setName("学校");
        Organization o1 = new Organization();
        o1.setName("计算机学院");
        Organization o2 = new Organization();
        o2.setName("数信学院");
        Organization o11 = new Organization();
        o11.setName("通信工程");
        Organization o21 = new Organization();
        o21.setName("应用数学");
        Organization o22 = new Organization();
        o22.setName("信息与计算科学");
       
        o.getChildren().add(o1);
        o.getChildren().add(o2);
        o1.getChildren().add(o11);
        o2.getChildren().add(o21);
        o2.getChildren().add(o22);
        o22.setParent(o2);
        o21.setParent(o2);
        o11.setParent(o1);
        o1.setParent(o);
        o2.setParent(o);
       
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        session.save(o);
        session.getTransaction().commit();
       
    }
   
    @Test
    public void testLoad() {
        testSave();
       
        Session session = sf.getCurrentSession();
        session.beginTransaction();
        Organization root = (Organization)session.get(Organization.class, 1);
        print(root , 0);
        session.getTransaction().commit();
    }

    private void print(Organization root , int level) {
        String str = "";
       
        for(int i = 0; i < level; i++) {
            str += "----";
        }
       
        if(root != null) {
            System.out.println(str + root.getName());

            Set<Organization> orgs = root.getChildren();
           
            if(orgs != null) {
                for(Organization org : orgs) {
                    print(org , level + 1);
                }
            }
        }
    }
}
 

你可能感兴趣的:(Hibernate,建立树形数据存储)