欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate多对多(ManyToMany)注解开发

程序员文章站 2022-04-23 15:41:32
...

hibernate多对多注解开发小Demo。上代码

Student.java

package com.it.hibernate.domain;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="c_student")
public class Student {
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Id
	//@Column(name="s_id")
	private Integer id;
	@Column(name="s_name")
	private String name;
	@ManyToMany(targetEntity=Teacher.class)
	// 使用JoinTabl来描述中间表,并描述中间表中外键与Student,Teacher的映射关系
	// joinColumns它是用来描述Student与中间表中的映射关系
	// inverseJoinColums它是用来描述Teacher与中间表中的映射关系
	@JoinTable(name="s_t",
	joinColumns={@JoinColumn(name="c_s_id",referencedColumnName="id")},
	inverseJoinColumns={@JoinColumn(name="c_t_id",referencedColumnName="id")}
			)
	private Set<Teacher> t = new HashSet<Teacher>();
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(Integer id, String name, Set<Teacher> t) {
		super();
		this.id = id;
		this.name = name;
		this.t = t;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Teacher> getT() {
		return t;
	}
	public void setT(Set<Teacher> t) {
		this.t = t;
	}
	
	

}

Teacher.java

package com.it.hibernate.domain;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="c_teacher")
public class Teacher {
	@Id
	@GenericGenerator(name="myuuid",strategy="uuid")
	@GeneratedValue(generator="myuuid") //引入自定义主键生成策略
	//@Column(name="t_id")
	private String id;
	@Column(name="t_name")
	private String name;
	@ManyToMany(targetEntity=Student.class,mappedBy="t") //让student维护外键表
	@Cascade(CascadeType.ALL)
	private Set<Student> students = new HashSet<Student>();
	public Teacher() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Teacher(String id, String name, Set<Student> students) {
		super();
		this.id = id;
		this.name = name;
		this.students = students;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	
	
	

}

1、测试级联保存

@Test
	public void test1() {
		// 获得持久化接口对象,并开启事务
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		// 操作
		Teacher t1 = new Teacher();
		t1.setName("张老师");
		Teacher t2 = new Teacher();
		t2.setName("李老师");

		Student s1 = new Student();
		s1.setName("张学生");
		Student s2 = new Student();
		s2.setName("李学生");

		// 维护外键(不执行外键维护的话,中间表没有数据)
		s1.getT().add(t1);
		s2.getT().add(t2);
		// 级联保存
		t1.getStudents().add(s1);
		t2.getStudents().add(s2);
		session.save(t1);
		session.save(t2);
		// 提交事务,并关闭持久化接口对象
		session.getTransaction().commit();
		session.close();

结果:

hibernate多对多(ManyToMany)注解开发

hibernate多对多(ManyToMany)注解开发

hibernate多对多(ManyToMany)注解开发


2、测试级联删除

	@Test
	public void del() {
		// 获得持久化接口对象,并开启事务
		Session session = HibernateUtils.openSession();
		session.beginTransaction();
		// 操作
		Student s = session.get(Student.class, 1);
		
		session.delete(s);
		// 提交事务,并关闭持久化接口对象
		session.getTransaction().commit();
		session.close();
	}

结果:

hibernate多对多(ManyToMany)注解开发

hibernate多对多(ManyToMany)注解开发

hibernate多对多(ManyToMany)注解开发

    总结:执行级联删除的时候(双向设置级联.all),执行删除某一个元素,只会删除该元素在其本身表中的数据和中间表中的级联数据。