Failed to lazily initialize role collection: com.pojo.Student.phonenos, session or session closed

I am studying hibernate mapping with annotation. I have completed one section. That is, I can insert the child class automatically when I save the parent table. see_that .

But I did not get the child table when I retrieve the main table. Also getting errors

failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed 

My code is added here for viewing. Please go through this. And give me some great advice. Student.java. (parent class)

 @Entity @Table(name="STUDENT") public class Student { private int studentid; private String studentName; private Set <Phone> phonenos; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="studenId") public int getStudentid() { return studentid; } public void setStudentid(int studentid) { this.studentid = studentid; } @Column(name="studenName") public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } @OneToMany(fetch = FetchType.LAZY) @JoinColumn(name="studenId") public Set<Phone> getPhonenos() { return phonenos; } public void setPhonenos(Set<Phone> phonenos) { this.phonenos = phonenos; } 

Phone.java (child class)

 @Entity @Table(name = "PHONE") public class Phone { private int phoneid; private String phoneNo; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "phoneId") public int getPhoneid() { return phoneid; } public void setPhoneid(int phoneid) { this.phoneid = phoneid; } @Column(name = "phoneno") public String getPhoneNo() { return phoneNo; } public void setPhoneNo(String phoneNo) { this.phoneNo = phoneNo; } 

my dao class

 public List<Student> getAllStudent() { List<Student> studentList = null; try { DetachedCriteria criteria = DetachedCriteria.forClass(Student.class); studentList = (List<Student>)getHibernateTemplate().findByCriteria(criteria); if(studentList != null && ! studentList.isEmpty()){ for(Student st :studentList){ System.out.println(" st name : "+st.getStudentName()); if(st.getPhonenos() != null && ! st.getPhonenos().isEmpty()){ for(Phone ph : st.getPhonenos()){ System.out.println(" ph no : "+ ph.getPhoneNo()); } }else{ System.out.println(" phone number is null"); } } }else{ System.out.println(" student null"); } } catch (DataAccessException e) { e.printStackTrace(); } return studentList; } 

Out put -

 failed to lazily initialize a collection of role: com.pojo.one2many.unidirectional.Student.phonenos, no session or session was closed 

Here I use a unidirectional (external) mapping to one to many (not a joint table, bidirectional).

Flying my question

1) how to get the child table when getting the parent table, vice versa

2) that of impatience and laziness.

3) unidirectional, bidirectional and combined table in the case of one-to-many display, for which more power.

+6
source share
1 answer

1)

If you want to really do this every time any entity of these classes is retrieved, specify FetchMode.EAGER in the @OneToMany association. @ManyToOne impatient by default. Keep in mind that this can be pretty much ineffective if you need to get these objects only in certain circumstances. If so, you should do it the way you do, but make sure that the session that the Student object received is still open. Seeing that you are using Spring, did you try to annotate the DAO / Service with @Transactional so that the session is saved during the execution of the method? Or you tried to use Hibernate.execute() , for example:

  getHibernateTemplate().execute(new HibernateCallback(){ @SuppressWarnings("unchecked") public Object doInHibernate(Session s) throws HibernateException, SQLException { Criteria c = s.createCriteria(Student.class); List<Student> studentList = c.list(); for(Student st :studentList){ st.getPhoneNos(); } } }); 

2) Take a look at this question: Difference between FetchType LAZY and EAGER in Java persistence? .

3) It depends on what you need. If you only need to navigate the association in one way, define it only in one direction. If you need both, do both. Join Table is more related to database design. If you want to have FK in the Phone table with a link to Student , to which the phone belongs, or you want to have a table of connections with Phones each Student .

+5
source

Source: https://habr.com/ru/post/899674/


All Articles