Hibernate多对多简单配置

@Entity
@Table(name="teacher",catalog="hibernate3")
public class Teacher implements Serializable {
	private Long tid;
	private String name;
	private Integer age;
	private Set<Course> courses=new HashSet<>();
     
	
	public Teacher() {
		super();
	}
	public Teacher( String name, Integer age) {
		super();
		this.name = name;
		this.age = age;
	}



	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)

    
	public Long getTid() {
		return tid;
	}

	public void setTid(Long tid) {
		this.tid = tid;
	}

	
	@Column(name="name",length=30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="age",length=4)
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

    /*
    * @JoinTable配置一个中间表
    * joinColumns:当前类在中间表的id列名
    * inverseJoinColumns:对方类在中间表的id列名
    */
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name="thacher_course",
    joinColumns=@JoinColumn(name="teacher_id"),
    inverseJoinColumns={@JoinColumn(name="course_id")})
	public Set<Course> getCourses() {
		return courses;
	}

	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}

}


Course类

@Entity
@Table(name="course",catalog="hibernate3")
public class Course implements Serializable {
	private Long cid;
	private String name;
	private Set<Teacher> teachers =new HashSet<>();
	
    public Course(String name) {
		super();
		this.name = name;
	}

	public Course() {
		super();
		
	}

	@Id
       @GeneratedValue  //默认AUTO--Native
  

	public Long getCid() {
		return cid;
	}

	public void setCid(Long cid) {
		this.cid = cid;
	}

	@Column(name="name",length=20)
	public String getName() {
		return name;
	}
  
	public void setName(String name) {
		this.name = name;
	}
	
	 /*
	 * mappedBy="courses",表示自已不维护id关系了,交给对方Thacher去维护
	 */
	@ManyToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy="courses")
	//抓取策略
	@Fetch(FetchMode.SELECT)
	public Set<Teacher> getTeachers() {
		return teachers;
	}

	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}

}


测试:双方最好要有
cascade=CascadeType.ALL

public class ManyToMany {
	   //课程添加老师
		@Test
		public void testName1() throws Exception {
	       Session session = HibernateUtils1.openSession();
	       Transaction trans = session.beginTransaction();
	       Teacher tt=new Teacher("zz11", 12);
	       
	       Course cc1=new Course("语文111");
	      
	       Set<Teacher> set=new HashSet<>();
	       set.add(tt);
	       //这里是课程添加老师,因为课程这边不维护关系,所以中间表没有记录的
           cc1.setTeachers(set);	       
	       session.save(cc1);
	       trans.commit();
	       session.close();
		}
		
		//老师添加课程
		@Test
		public void testName111() throws Exception {
			Session session = HibernateUtils1.openSession();
			Transaction trans = session.beginTransaction();
			Teacher tt=new Teacher("aa1", 12);
			Course cc1=new Course("奥数1");
			
			Set<Course> set=new HashSet<>();
			set.add(cc1);
			//老师添加课程,老师需要维护关系(写入中间表)
			tt.setCourses(set);	   
			session.save(tt);
			trans.commit();
			session.close();
		}
		
		
		@Test
		public void testName3() throws Exception {
			Session session = HibernateUtils1.openSession();
			Transaction trans = session.beginTransaction();
			//一个老师对象
			Teacher tt = new Teacher("小wang11",11);
			session.save(tt);
			//一个课程对象
			Course course = new Course("数学1111");
			session.save(course);
			//老师添加课程,维护中间表
			tt.getCourses().add(course);
			//课程添加老师,不会维护中间表
//			course.getTeachers().add(tt);
			trans.commit();
			session.close();
		}
}



你可能感兴趣的:(多对多)