gstackoverflow
gstackoverflow

Reputation: 37034

org.hibernate.LazyInitializationException: could not initialize proxy - no Session when I use EAGER initalization

I have hibernate mapping:

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;
    }
}

and Student:

@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;
    }
}

invocation code:

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

When I invoke code above I see next trace:

Exception in thread "main" org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:164) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:285) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) at logic.Prepod_$$_javassist_1.setStudents(Prepod_$$_javassist_1.java) at logic.Main.main(Main.java:38)

What is the cause of this problem?

The state of my objects is detached in two last rows?

How to fix it?

UPDATE

after good advice for editing code from Prabhakaran I have next question:

If I update student - note doesn't added to intermediate table of database but another behavior if I make update for prepod entity. It was happened because prepod is owner of relationship?

Upvotes: 1

Views: 3053

Answers (1)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

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

Upvotes: 3

Related Questions