org.hibernate.LazyInitializationException:无法初始化代理 - 当我使用EAGER初始化时没有会话

时间:2013-09-12 11:37:48

标签: java hibernate jpa orm

我有hibernate映射:

Prepod:

@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);
....

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

  

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

这个问题的原因是什么?

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

如何解决?

更新

在编辑Prabhakaran的代码后,我有下一个问题:

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

1 个答案:

答案 0 :(得分: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);