Hibernate學習系列————註解多對多實例

需求分析:學生和教師之間存在多對多的關係java

Student2類:數據庫

package cn.dao;session

 

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;fetch

 

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
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;this

 

@Entity
@Table (name = "student2")
public class Student2 implements Serializable{
 
 private Long id;
 
 private String name;
 
 private String description;
 
 private Set<Teacher> teachers = new HashSet<Teacher>();.net

 

 @Id
 @GeneratedValue (strategy = GenerationType.AUTO)
 public Long getId() {
  return id;
 }hibernate

 

 public void setId(Long id) {
  this.id = id;
 }ip

 

 @Column (name = "name")
 public String getName() {
  return name;
 }rem

 

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

 

 @Column (name = "description")
 public String getDescription() {
  return description;
 }

 

 public void setDescription(String description) {
  this.description = description;
 }

 

 @ManyToMany(fetch = FetchType.EAGER)
 @JoinTable(name = "teacher_student",joinColumns={ @JoinColumn(name = "studentId" ,nullable = false, updatable = false)},inverseJoinColumns={@JoinColumn (name = "teacherId",nullable = false,updatable = false)})
 public Set<Teacher> getTeachers() {
  return teachers;
 }

 

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

 

}

 

Teacher 類:

package cn.dao;

 

import java.io.Serializable;
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 = "teacher")
public class Teacher implements Serializable {
 
 private Long id;
 
 private String name;
 
 private String description;
 
 private Set<Student2> students = new HashSet<Student2>();

 

 @Id
 @GeneratedValue (strategy = GenerationType.AUTO)
 public Long getId() {
  return id;
 }

 

 public void setId(Long id) {
  this.id = id;
 }

 

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

 

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

 

 @Column(name = "description")
 public String getDescription() {
  return description;
 }

 

 public void setDescription(String description) {
  this.description = description;
 }

 

 @ManyToMany
 @JoinTable(name = "teacher_student",joinColumns={ @JoinColumn(name = "teacherId",nullable = false,updatable = false)},inverseJoinColumns={ @JoinColumn( name = "studentId",nullable = false,updatable = false)})
 public Set<Student2> getStudents() {
  return students;
 }

 

 public void setStudents(Set<Student2> students) {
  this.students = students;
 }
 
 

 

}

 

ManyToMany類:

package cn.test;

 

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

 

import org.hibernate.Session;
import org.hibernate.Transaction;

 

import cn.dao.Student2;
import cn.dao.Teacher;
import cn.utils.HibernateUtil;

 

/**  * 多對多的操做  * @author hasee  *  */ public class ManyToMany {    public static void main(String[] args) {   edit();  }    public static void save(){   //一個學生,擁有多個老師,一個老師,也擁有多個學生   Session session = HibernateUtil.getSession();   Transaction transcation = session.beginTransaction();   Student2 studentOne = new Student2();   studentOne.setName("Amumu");   studentOne.setDescription("This is Amumu");   Set<Teacher> teachers = new HashSet<Teacher>();   Teacher t1 = new Teacher();   Teacher t2 = new Teacher();   t1.setName("Teacher Wang");   t1.setDescription("Teacher Wang is Women");   t2.setName("Teacher Chen");   t2.setDescription("Teacher Chen is Man");   teachers.add(t1);   teachers.add(t2);   studentOne.setTeachers(teachers);   session.save(t1);   session.save(t2);   session.save(studentOne);   transcation.commit();   HibernateUtil.closeSession();     }    public static void save2(){   //添加一個學生時,爲其添加指定的老師(分析:現數據庫裏有ID爲1和2的老師,把這個學生添加到老師1中)   Session session = HibernateUtil.getSession();   Transaction transcation = session.beginTransaction();   Student2 student = new Student2();   student.setName("Jary");   student.setDescription("Jary is a new Student");   //此時Jary指定要到ID爲1的老師   Set<Teacher> teachers = new HashSet<Teacher>();   Teacher teacher = (Teacher) session.get(Teacher.class, 12L);   teachers.add(teacher);   student.setTeachers(teachers);//有點一對多的意思   session.save(student);   transcation.commit();   HibernateUtil.closeSession();  }    public static void edit(){   //根據學生的ID,改變和原有老師的關聯關係   Session session = HibernateUtil.getSession();   Transaction transcation = session.beginTransaction();   Student2 student = (Student2) session.get(Student2.class, 10L);//根據ID查出當前的學生   //查出當前的學生擁有幾個老師,打印出老師的名字和ID      Set<Teacher> teachers = student.getTeachers();   List<Teacher> list=new ArrayList<Teacher>(teachers);   for (Teacher teacher : list) {    System.out.println("老師的ID爲:"+teacher.getId()+"    老師的名字爲:"+teacher.getName());    //解除ID爲12,11的老師關聯    if(teacher.getId()==12 || teacher.getId()==11){     teachers.remove(teacher);    }   }   teachers = new HashSet<>(list);   session.update(student);   transcation.commit();   HibernateUtil.closeSession();  }    public static void delete(){   Session session = HibernateUtil.getSession();   Transaction transcation = session.beginTransaction();   //直接根據學生ID刪除學生  或者  根據 老師ID刪除老師 //  Student2 student = (Student2) session.get(Student2.class, 4L); //  session.delete(student); //  Teacher teacher = (Teacher) session.get(Teacher.class, 3L); //  session.delete(teacher);   //刪除ID爲4的老師下的所有學生   Teacher teacher = (Teacher) session.get(Teacher.class, 4L);   Set<Student2> students = teacher.getStudents();   for (Student2 student2 : students) {    session.delete(student2);   }   transcation.commit();   HibernateUtil.closeSession();  } }

相關文章
相關標籤/搜索