Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6709259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:51:35+00:00 2026-05-26T07:51:35+00:00

I am learning hibernate mapping using annotation. I have completed one section. I.e. I

  • 0

I am learning hibernate mapping using annotation. I have completed one section. I.e. I can insert child class automatically when I save the parent table.
see_that.

But I did n’t get the child table when I am fetching the master table. Also getting an error

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 to review you. Please go through it. And give me the 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 is

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

Here I am using unidirectional (foreign key) one-To-many mapping (not joint table, bidirectional).

Summerizing my question

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

2) what is eager and lazy fetch.

3) unidirection, bidirection, and join table in the case of One-To-Many mapping which one is more power full.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:51:36+00:00Added an answer on May 26, 2026 at 7:51 am

    1)

    If you want really to do this every time that any entity of these classes is retrieved, specify FetchMode.EAGER in the @OneToMany association. @ManyToOne are eager by default. Be aware that this might be largely unefficient if you need to fetch those entities only in particular circumstances. If that’s the case, you have to do it like you’re doing, but make sure that the session that retrieved the Student object is still open. Seeing that you’re using Spring, have you tried annotating your DAO/Service with @Transactional, so that the session keeps alive during method execution? Or have you tried using Hibernate.execute(), like this:

     

       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 unidirectional in that way only. If you need both, do both. Join Table has more to do with database design. If you want to have a FK in the Phone table with the reference to the Student the phone belongs to, or you want to have a join table with the Phones of every Student.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been learning Hibernate for the past few weeks, I have gotten most
I am learning Hibernate from the tutorials on JBoss website. I have a confusion
I'm using hibernate with hbm2ddl.auto=update so that it'll automatically generate my oracle tables for
I am just learning NHibernate. I have been using examples from the documentation and
I'm still learning what hibernate can do and this time i'm trying something that
I'm learning Hibernate Validator. I see that I can add an error message text
I am learning hibernate and have a question regarding the design of my POJO
I have been learning java spring hibernate MVC for 3 months and got pretty
I have been learning to use hibernate for a couple of months. I am
I am learning spring mvc and hibernate. i have just finished simple site with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.