Hibernate中的多对一和多对多关系

1.多个Person对应一个department的情况

   person类:

  

public class Person {
  
	private String pno;
	private String name;
	private String address;
	private int age;
	private Department dept;
	
	public Department getDept() {
		return dept;
	}
	public void setDept(Department dept) {
		this.dept = dept;
	}
	public String getPno() {
		return pno;
	}
	public void setPno(String pno) {
		this.pno = pno;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}
    person类对应的person.hbm.xml的配置

    





	
		
		 
			
		
	    
	    
	    
	    
	    
	    
		
	
	
    

         department 类:

   

	private String deptno;
	private String deptname;
	private String deptaddress;

	public String getDeptno() {
		return deptno;
	}
	public void setDeptno(String deptno) {
		this.deptno = deptno;
	}
	public String getDeptname() {
		return deptname;
	}
	public void setDeptname(String deptname) {
		this.deptname = deptname;
	}
	public String getDeptaddress() {
		return deptaddress;
	}
	public void setDeptaddress(String deptaddress) {
		this.deptaddress = deptaddress;
	}
	 
   department.hbm.xml的配置 :

      





	
		
		
			
		
	    
	     
	     
	
	
	

    然后就是在 Junit中对其进行测试,首先添加表:

      

public void createTable(){
		Configuration cfg = new Configuration().configure();//读hibernate配置文件
		SchemaExport se = new SchemaExport(cfg);//获取创建表的对象
		se.create(true, true);//创建表
	} 
   然后在插入数据:

   

public void AddData(){
		Configuration cfg = new Configuration().configure();//读取Hibernate配置文件
		SessionFactory  sessionfactory = cfg.buildSessionFactory();//获取会话工厂
		Session session = sessionfactory.openSession();//打开会话工厂(获取会话)
		
		Transaction ts = session.beginTransaction();//开启事务
		Department dept = new Department();
		dept.setDeptname("计生局");
		dept.setDeptaddress("眉山");
		
		Person person = new Person();//创建对象
		person.setName("niam");
		person.setAddress("asdfasdf");
	    person.setAge(12);
	    person.setDept(dept);
	    
	    Person person1 = new Person();//创建对象
		person1.setName("niam111");
		person1.setAddress("asdfasdf111");
	    person1.setAge(12);
	    person1.setDept(dept);

	    /*
	     * 如果在
	    中配置了 cascade="save-update"的话此处就
	            不必执行  session.save(dept);(保存部门先保证多对一中的一),否则就必须
	             先保存 多对一中的一                                                                                                                  */
	    session.save(dept);
	    session.save(person1);
        session.save(person);//将对象存到会话中    
	    ts.commit();//手动提交事务
	    session.close();
	}
 


     在介绍多对多的关系,其实也很简单,只需要在deparment的实体中添加一个 Set persons属性  eg:

    

	private Set persons;
	
	public Set getPersons() {
		return persons;
	}
	public void setPersons(Set persons) {
		this.persons = persons;
	}
  

然后在配置文件中加上 :

    


	         
	         
	     
   然后就是在测试类中对其进行迭代输出:

  (获取的时候都是经过Id 很纠结的一串,就不写了)  

    

for(Object p:dept.getPersons().toArray()){
			Person p1 = (Person)p;
			System.out.println("p.name:"+p1.getName());
		}

你可能感兴趣的:(Hibernate中的多对一和多对多关系)