我的java历程

Hibernate EntityManager 多对多持久化

在应用Hibernate EntityManager进行多对多持久化时,运行TeacherManager时可以同时将Teacher,及Student数据都保存进数据库,可运行StudentManager时,却只能将Student数据保存,而Teacher数据并没有保存到数据库,为什么只能单向持久化,感觉只实现了一对多双向关联的功能,为此感到不解,请热心的大哥们帮忙解答,看我的代码是不是哪里写错了?谢谢。
以下是我的代码:

package com.landril.bean;

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

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
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 static final long serialVersionUID = 7772760144352800638L;

 @Id
 @GeneratedValue
 private Integer teacherID;

 private String teacherName;
 
 @ManyToMany(cascade=CascadeType.PERSIST,fetch=FetchType.EAGER)
 @JoinTable(name="Tea_Stud",
   joinColumns={@JoinColumn(name="teacherID",referencedColumnName="teacherID")},
   inverseJoinColumns={@JoinColumn(name="studentID",referencedColumnName="studentID")})
 private Set<Student> students = new HashSet<Student>();

 public Integer getTeacherID() {
  return teacherID;
 }

 public void setTeacherID(Integer teacherID) {
  this.teacherID = teacherID;
 }

 public String getTeacherName() {
  return teacherName;
 }

 public void setTeacherName(String teacherName) {
  this.teacherName = teacherName;
 }

 public Set<Student> getStudents() {
  return students;
 }

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



package com.landril.bean;

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

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

@Entity
@Table(name = "Student")
public class Student implements Serializable {

 private static final long serialVersionUID = -2778169665228730867L;

 @Id
 @GeneratedValue
 private Integer studentID;

 private String studentName;

 @ManyToMany(mappedBy="students")
 private Set<Teacher> teachers = new HashSet<Teacher>();

 public Integer getStudentID() {
  return studentID;
 }

 public void setStudentID(Integer studentID) {
  this.studentID = studentID;
 }

 public String getStudentName() {
  return studentName;
 }

 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }

 public Set<Teacher> getTeachers() {
  return teachers;
 }

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



package com.landril.dao;

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

import javax.persistence.EntityManager;

import com.landril.bean.Student;
import com.landril.bean.Teacher;

public class TeacherManager {
 
 public static void main(String[] args) {
  Teacher t = new Teacher();
  Student s = new Student();
  s.setStudentName("aa");
  Student s1 = new Student();
//  StudentManager sm = new StudentManager();
//  Student s1 = sm.getStudent(2);
  s1.setStudentName("bb");
  Student s2 = new Student();
  s2.setStudentName("cc");
  
  Set<Student> ss = new HashSet<Student>();
  ss.add(s);
  ss.add(s1);
  ss.add(s2);
  t.setTeacherName("tt");
  t.setStudents(ss);
  
  Teacher t1 = new Teacher();
  t1.setTeacherName("tt");
  t1.setStudents(ss);
  
  Teacher t2 = new Teacher();
  t2.setTeacherName("tt");
  t2.setStudents(ss);
  
  TeacherManager tm = new TeacherManager();
  tm.saveTeacher(t);
  tm.saveTeacher(t1);
  tm.saveTeacher(t2);

 }
 
 public void saveTeacher(Teacher tea){
  EntityManager em = Manager.emf.createEntityManager();
  em.getTransaction().begin();
  em.persist(tea);
  em.getTransaction().commit();
  em.close();
 }
 
 public Teacher getTeacher(int id){
  EntityManager em = Manager.emf.createEntityManager();
  return em.find(Teacher.class, new Integer(id));
 }
}


package com.landril.dao;

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

import javax.persistence.EntityManager;

import com.landril.bean.Student;
import com.landril.bean.Teacher;

public class StudentManager {

 public static void main(String[] args) {
//  StudentManager sm = new StudentManager();
//  Student s = sm.getStudent(2);
//  System.out.println(s.getStudentName());
  
  TeacherManager tm = new TeacherManager();
  Teacher t1 = new Teacher();
  t1.setTeacherName("t1");
  Teacher t2 = new Teacher();
  t2.setTeacherName("t2");
  Teacher t3 = new Teacher();
  t3.setTeacherName("t3");
//  Teacher t1 = tm.getTeacher(1);
//  Teacher t2 = tm.getTeacher(3);
//  Teacher t3 = tm.getTeacher(5);
  Set<Teacher> ts = new HashSet<Teacher>();
  ts.add(t1);
  ts.add(t2);
  ts.add(t3);  
  
  Student s = new Student();
  s.setStudentName("ssss");
  s.setTeachers(ts);
  
  StudentManager sm = new StudentManager();
  sm.saveStudent(s);
 }
 
 public void saveStudent(Student s){
  EntityManager em = Manager.emf.createEntityManager();
  em.getTransaction().begin();
  em.persist(s);
  em.getTransaction().commit();
  em.close();
 }
 
 public Student getStudent(int id){
  EntityManager em = Manager.emf.createEntityManager();
  return em.find(Student.class, 1);
 }
}

posted on 2006-11-06 23:39 landril 阅读(3022) 评论(1)  编辑  收藏

Feedback

# re: Hibernate EntityManager 多对多持久化 2007-05-22 15:37 雷克斯

在你的Student Entity bean 的
@ManyToMany里加入cascade=CascadeType.PERSIST,fetch=FetchType.EAGER
这样就实现了Many-to-Many的关系了  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿(1)

随笔分类(18)

随笔档案(21)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜