1

我有休眠映射:

前脚架:

@Entity
@Table(name = "prepod")
public class Prepod {

    private Long id;
    private String name;
    @Column
    public String getName() {
        return name;
    }

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

    List<Student> students = new ArrayList<Student>();

    @ManyToMany(fetch=FetchType.EAGER)
    public List<Student> getStudents() {
        return students;
    }

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

    public void setId(Long i) {
        id = i;
    }
}

和学生:

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

    private Long id;
    private String name;
    private Long age;
    private List<Prepod> prepods = new ArrayList<Prepod>();

    @ManyToMany(mappedBy = "students",fetch=FetchType.EAGER)
    public List<Prepod> getPrepods() {
        return prepods;
    }

    public void setPrepods(List<Prepod> prepods) {
        this.prepods = prepods;
    }

    public Student() {
        name = null;
    }

    public Student(Student s) {
        name = s.getName();
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public Long getId() {
        return id;
    }

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

    @Column(name = "age")
    public Long getAge() {
        return age;
    }

    public void setId(Long i) {
        id = i;
    }

    public void setName(String s) {
        name = s;
    }

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

调用代码:

Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Prepod prepod = (Prepod) session.load(Prepod.class, 1l);
        Student student = (Student) session.load(Student.class, 1l);
        session.getTransaction().commit();
        session.flush();
        session.close();
                List<Student> students = new ArrayList<Student>();
        students.add(student);

        List<Prepod> prepods = new ArrayList<Prepod>();
        prepods.add(prepod);

        prepod.setStudents(students);//exception here
        student.setPrepods(prepods);
....

当我调用上面的代码时,我看到了下一个跟踪:

线程“主”org.hibernate.LazyInitializationException 中的异常:无法初始化代理 - org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164) 处没有会话。 :285) 在 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) 在 logic.Prepod_$$_javassist_1.setStudents(Prepod_$$_javassist_1.java) 在 logic.Main.main(Main.爪哇:38)

这个问题的原因是什么?

我的对象的状态在最后两行中分离?

如何解决?

更新

在从 Prabhakaran 编辑代码的好建议之后,我有下一个问题:

如果我更新学生 - 注释不会添加到数据库的中间表中,但如果我对 prepod 实体进行更新,则会出现另一种行为。发生这种情况是因为 prepod 是关系的所有者?

4

1 回答 1

3
    Prepod prepod = null;
    Student  student = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    prepod = (Prepod) session.load(Prepod.class, 1l);
    student = (Student) session.load(Student.class, 1l);
    session.getTransaction().commit();
    session.flush();
    session.close();
            List<Student> students = new ArrayList<Student>();
    students.add(student);

    List<Prepod> prepods = new ArrayList<Prepod>();
    prepods.add(prepod);

    prepod.getStudents().addAll(students);//exception here
    student.getPrepods().addAll(prepods);
于 2013-09-12T11:53:02.183 回答