Hibernate 双向多对多&拆分成两个一对多

纸上得来终觉浅

1.双向多对多:B(Teacher)有多个A(Student),A也有多个B

注意要在一方加上inverse属性,否则一起维护关联关系可能会造成主键冲突:

package roadArchitectWeb;

import java.util.HashSet;
import java.util.Set;

public class Teacher {
	private Integer Id;
	private String Name;
	private String PhoneNum;
	private Set Students = new HashSet<>();

	public Set getStudents() {
		return Students;
	}
	public void setStudents(Set students) {
		Students = students;
	}
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getName() {
		return Name;
	}
	public void setName(String name) {
		Name = name;
	}
	public String getPhoneNum() {
		return PhoneNum;
	}
	public void setPhoneNum(String phoneNum) {
		PhoneNum = phoneNum;
	}
	public  Teacher() {
	}
	@Override
	public String toString() {
		return "Teacher [Id=" + Id + ", Name=" + Name + ", PhoneNum=" + PhoneNum + ", Students=" + Students + "]";
	}
}

package roadArchitectWeb;

import java.util.HashSet;
import java.util.Set;

public class Student {
	private Integer Id;
	private String sName;
	private String Age;
	private Set Teachers = new HashSet<>();
	public Set getTeachers() {
		return Teachers;
	}
	public void setTeachers(Set teachers) {
		Teachers = teachers;
	}
	public Integer getId() {
		return Id;
	}
	public void setId(Integer id) {
		Id = id;
	}
	public String getsName() {
		return sName;
	}
	public void setsName(String sName) {
		this.sName = sName;
	}
	public String getAge() {
		return Age;
	}
	public void setAge(String age) {
		Age = age;
	}
	@Override
	public String toString() {
		return "Student [Id=" + Id + ", sName=" + sName + ", Age=" + Age + ", Teachers=" + Teachers + "]";
	}
	public Student(){
	}
}




    
        
            
            
            
        
        
            
        
        
            
        
       
        
        
        
    




    
        
            
            
            
        
        
            
        
        
            
        
       
        
        
        
    
package roadArchitectWeb;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor;
public class HibernateTest {
	@Test
	public void test() {
		SessionFactory sessionFactory = null;
		Configuration configuration = new Configuration().configure();
		sessionFactory = configuration.buildSessionFactory();
		Session session = sessionFactory.openSession();
		Transaction transaction = session.beginTransaction();
		
		/*begin*/
		Teacher teacher = new Teacher();
		Student student = new Student();
		teacher.setName("chenT");
		teacher.setPhoneNum("18788837117");
		student.setsName("ma");
		student.setAge("11");
		
		student.getTeachers().add(teacher);
		teacher.getStudents().add(student);
		
		session.save(student);
		session.save(teacher);
		
//		student = (Student)session.get(Student.class, 1);
//		teacher = (Teacher)session.get(Teacher.class,1);
//		System.out.println("HibernateTest.test()S:"+student.getTeacher().getName());
//		System.out.println("HibernateTest.test()T:"+teacher.getStudents().size());
		
		transaction.commit();
		session.close();
		sessionFactory.close();
	}
}
这个时候的双向多对多,数据库会生成一个关联表,里面有且只有两个类的外键。  

2.多对多拆分两个一对多

针对上面那个情况,因为自动生成的关联表中只有两个外键这两个列,如果我想在这个列种存放其他属性怎么办? 因为关联表是自动生成的,这个时候的解决办法就是把多对多的关联关系改为两个一对多双向关联。  举个例子:

一个订单有多种商品,种个商品可能在多个订单里。但是我如果描述一个订单里一种商品的数量该怎么描述,必须建立一个订单-商品关联表,然后在关联表中设置商品的数量,这样一条记录就代表一个订单,一种商品和这个商品的数量。

那应该如何实现呢?   把订单当作一,把商品当作一,把关系表(这个关系表与订单以及商品一样是通过一个类生成的,而不是hibernate自动生成)当作多,并且建立两个一对多的双向关联关系,就可以实现。 

代码实现按照之前的一对多双向示例即可。

你可能感兴趣的:(Hibernate)