我正在使用 Hibernate 3.2,而且负载很正常。这是我的问题。
package entity;
import java.util.Date;
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.OneToMany;
@Entity
public class Teacher {
private int id;
private String name;
private Set<Student> students = new HashSet<Student>();
private Date time;
public Teacher() {
super();
}
public Teacher(int id, String name, Set<Student> students, Date time) {
super();
this.id = id;
this.name = name;
this.students = students;
this.time = time;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getName() {
return name;
}
@OneToMany(mappedBy="teacher",cascade={CascadeType.ALL},fetch=FetchType.EAGER)
@JoinColumn(name="teacher_id")
public Set<Student> getStudents() {
return students;
}
public Date getTime() {
return time;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
public void setTime(Date time) {
this.time = time;
}
@Override
public String toString() {
return "Teacher+"+this.name+",student number:"+this.students.size();
}
}
Teacher.java
另一个班级
package entity;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student implements Cloneable{
private Date brith;
private int id;
private Teacher teacher;
public Student() {
super();
}
public Student(Date brith, int id, Teacher teacher) {
super();
this.brith = brith;
this.id = id;
this.teacher = teacher;
}
public Date getBrith() {
return brith;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
@ManyToOne(cascade={CascadeType.ALL},fetch=FetchType.LAZY)
public Teacher getTeacher() {
return teacher;
}
public void setBrith(Date brith) {
this.brith = brith;
}
public void setId(int id) {
this.id = id;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
@Override
public String toString() {
return "student brith="+this.brith+"teacher="+teacher.getName();
}
}
Student.java
这是我的测试代码,
package entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class Hibernate {
static SessionFactory sessionFactory;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
sessionFactory.close();
}
@Test
public void testSaveStudent(){
Student s = new Student();
s.setBrith(new Date());
Student s1 = new Student();
s1.setBrith(new Date());
Teacher t = new Teacher();
t.setName("teacher");
t.setTime(new Date());
Set<Student> set = new HashSet<Student>();
set.add(s1);
set.add(s);
t.setStudents(set);
s1.setTeacher(t);
s.setTeacher(t);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s1);
session.getTransaction().commit();
}
@Test
public void testFetchStudent(){
testSaveStudent();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s = (Student)session.load(Student.class, 1);
s.getBrith();
session.getTransaction().commit();
System.out.println(s);
}
@Test
public void testFetchStudent2(){
testSaveStudent();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Student s = (Student)session.load(Student.class, 1);
System.out.println(s);
session.getTransaction().commit();
System.out.println(s);
}
@Test
public void testFetchTeacher(){
testSaveStudent();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
Teacher t = (Teacher)session.load(Teacher.class, 1);
/*System.out.println(t);
t.setName("what are you doing");
session.update(t);*/
session.getTransaction().commit();
System.out.println(t);
}
public static void main(String[] args) throws Exception {
setUpBeforeClass();
}
}
让它更容易,方法 testStudent 失败,但 testStudent2 成功,session.load 方法有什么魔力?为什么他们不能得到相同的结果?