hibernate 树状展示

实体类


package com.eagle.hibernate.model;

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.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

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




测试类
package com.eagle.hibernate.model;

import java.util.Date;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;


public class HibernateCoreAPI {
private static SessionFactory sf;
	
	@BeforeClass
	public static void buildSession(){

		try {
			new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
			sf = new AnnotationConfiguration().configure().buildSessionFactory();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@AfterClass
	public static void closeSession(){
		sf.close();
	}
	@Test
	public void testSchemaExport(){
		new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);
		
	}
	@Test
	public void testSaveCompany(){
		Company company = new Company();
		company.setName("总公司");
		Company company1 = new Company();
		company1.setName("子公司1");
		Company company2 = new Company();
		company2.setName("子公司2");
		
		Company company3 = new Company();
		company3.setName("子公司3");
		
		Company company4 = new Company();
		company4.setName("子子公司2_1");
		Company company5 = new Company();
		company5.setName("子子公司2_2");
		Company company6 = new Company();
		company6.setName("子子公司2_3");
		Company company7 = new Company();
		company7.setName("子子公司3_1");
		
		company.getChildren().add(company1);
		company.getChildren().add(company2);
		company.getChildren().add(company3);
		company1.setParent(company);
		company2.setParent(company);
		company3.setParent(company);
		
		company2.getChildren().add(company4);
		company2.getChildren().add(company5);
		company2.getChildren().add(company6);
		company4.setParent(company2);
		company5.setParent(company2);
		company6.setParent(company2);
		
		company3.getChildren().add(company7);
		company7.setParent(company3);
	Session s = sf.getCurrentSession();
	s.beginTransaction();
	s.save(company);
	s.getTransaction().commit();
	
	
	}
	
	@Test
	public void testLoad(){
		 testSaveCompany();
		 Session s = sf.getCurrentSession();
		 s.beginTransaction();
		 Company c = (Company)s.get(Company.class, 1);
		 print(c,0);
		 s.getTransaction().commit();
	}

	private void print(Company c,int level) {
		String str = "";
		for(int i=0;i<level;i++){
			str += "----";
		}
		System.out.println(str + c.getName());
		for(Company company:c.getChildren()){
			print(company,level+1);
		}
		
	}
	
}

你可能感兴趣的:(java,C++,c,Hibernate,JUnit)