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

  • Home
  • SEARCH
  • 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 9182109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:57+00:00 2026-06-17T18:28:57+00:00

Student.java package Model; import java.util.Set; public class Student { private int studentID; private String

  • 0

Student.java

package Model;


import java.util.Set;

public class Student {
private int studentID;
private  String name;
private String city;
private Set<Address> AddressList;
private Set<Contact> ContactList;

public Set<Contact> getContactList() {
    return ContactList;
}

public void setContactList(Set<Contact> contactList) {
    ContactList = contactList;
}

public Set<Address> getAddressList() {
    return AddressList;
}

public void setAddressList(Set<Address> addressList) {
    AddressList = addressList;
}

public int getStudentID() {
    return studentID;
}

public void setStudentID(int studentID) {
    this.studentID = studentID;
}

public String getName() {
    return name;
}

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

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

}

Address.java

package Model;

public class Address {

private int Aid;
private String fullAddress;
//private Student student;
/*public Student getStudent() {
    return student;
}
public void setStudent(Student student) {
    this.student = student;
}*/
public int getAid() {
    return Aid;
}
public void setAid(int aid) {
    Aid = aid;
}
public String getFullAddress() {
    return fullAddress;
}
public void setFullAddress(String fullAddress) {
    this.fullAddress = fullAddress;
}
}

Contact.java

package Model;

public class Contact {
private int contactId;
private String mobileNo;

public int getContactId() {
    return contactId;
}
public void setContactId(int contactId) {
    this.contactId = contactId;
}
public String getMobileNo() {
    return mobileNo;
}
public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
}



 }

Student.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Model.Student" table="Student">
    <id name="studentID" type="int">
        <column name="StudentID" />
        <generator class="identity" />
    </id>
    <property name="name" type="java.lang.String"/>
    <property name="city" type="java.lang.String"/>
    <set name="AddressList">
        <key column="studentID"/>
        <one-to-many class="Model.Address"/>        
    </set>
    <set name="ContactList">
        <key column="studentID"/>
        <one-to-many class="Model.Contact"/>        
    </set>
</class>

Address.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Model.Address" table="Address">
    <id name="Aid" type="int">
        <column name="Aid" />
        <generator class="identity" />
    </id>
    <property name="fullAddress" type="java.lang.String"/>
</class>

Contact.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Model.Contact" table="Contact">
    <id name="contactId" type="int">
        <column name="contactId" />
        <generator class="identity" />
    </id>
    <property name="mobileNo" type="java.lang.String"/>
</class>

Main method.

public static void main(String[] args)
{
    Configuration cfg=new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SchemaExport se=new SchemaExport(cfg);
    se.setOutputFile("D:\\abc.sql");
    se.create(true,true);//create(boolean script, boolean export) 

    System.out.println("------------------Saving of Student--------------------");
    Student student =new Student();
    student.setName("Yograj");
    student.setCity("Nanded");
    Set<Address> addressList=new HashSet<Address>();
    Address address=new Address();
    address.setFullAddress("Chikhali Nanded");
    addressList.add(address);
    address=null;
    address=new Address();
    address.setFullAddress("karvenagar Pune");
    addressList.add(address);
    student.setAddressList(addressList);

    Set<Contact> contactList=new HashSet<Contact>();
    Contact contact=new Contact();
    contact.setMobileNo("9403330577");
    contactList.add(contact);
    contact=null;
    contact=new Contact();
    contact.setMobileNo("9890864805");
    contactList.add(contact);
    student.setContactList(contactList);

    boolean result=new StudentServices().SaveStudent(student);

    if(result)
    {
        System.out.println("------------------Save Student succ..--------------------");
    }
    else
    {
        System.out.println("------------------Saving failed--------------------");
    }

    System.out.println("------------------Student Details--------------------");
    List<Student> studentList=new StudentServices().GetStudentlist();
    for (Student stud : studentList) {
        System.out.println("Name:"+stud.getName());
        System.out.println("City:"+stud.getCity());
        for (Address add : stud.getAddressList()) {
            System.out.println("Full Address:"+add.getFullAddress());
        }
        for (Contact cont : stud.getContactList()) {
            System.out.println("Full Address:"+cont.getMobileNo());
        }
    }
    System.out.println("Execution over.");


}

StudentService.Java

package Service;

import java.util.List;

import javassist.runtime.Inner;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;

import Model.Address;
import Model.Contact;
import Model.Student;

public class StudentServices {
public boolean SaveStudent(Student student) {
    try {
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        Transaction transaction = session.beginTransaction();

        session.save(student);
        for (Address add : student.getAddressList()) {
            session.save(add);

        }
        for (Contact cont : student.getContactList()) {
            session.save(cont);

        }
        transaction.commit();
        session.close();
        return true;
    } catch (HibernateException e) {
        return false;
    }
}

public List<Student> GetStudentlist() {
    try {
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        Transaction transaction = session.beginTransaction();
        Criteria criteria = session.createCriteria(Student.class, "Student")
                .createAlias("Student.AddressList", "Address",JoinType.INNER_JOIN)
                .createAlias("Student.ContactList", "Contact",JoinType.INNER_JOIN);
        List<Student> studentList = criteria.list();

        transaction.commit();
        session.close();
        return studentList;
    } catch (HibernateException e) {
        return null;
    }
}
}

When i execute main method hibernate will create database and table.in all table it will store records but when i fetch records using criteria API it gives me error “org.hibernate.LazyInitializationException.”

  • 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-06-17T18:28:59+00:00Added an answer on June 17, 2026 at 6:28 pm

    In your case student.addressList and student.contactList use lazy loading. Lazy loading means, the values are not loaded together with the studentList, but only then when you access them with getAddressList()and getContactList.

    Lazy loading only works as long as the session is still open. If not yet loaded components are accessed after closing the session a LazyInitializationException is thrown, as in your example.

    What you are doing is:

    1. in GetStudentlist: open the session
    2. in GetStudentlist: load all students
    3. in GetStudentlist: close the session
    4. in main: for each student
    5. in main: access the address list via student.getAddressList()

    This throws in exception in step 5, because you access the address list, which applies to lazy loading, and the data can’t be loaded any more, because the session already was closed in step 3.

    You must close the session after calling getAddressList() and getContactList(), then it works.

    By the way, when you only read data as in GetStudentlist, then you do not need to begin a transaction, and you do not need to commit anything.

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

Sidebar

Related Questions

public class Student implements java.io.Serializable { private long studentId; private String studentName; private Set<Course>
I'm trying to calculate the average of a student's marks: import java.util.Scanner; public class
package homework1; import java.applet.Applet; import java.awt.Graphics; public class Homework1A extends Applet { public void
I have the following Java class: package web; import javax.servlet.*; import javax.servlet.http.*; import java.io.*;
package MyTest; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; class Person { ... } class
A simple java program to input the name of student and find the average
I have Two Class in school package Class school package school; public class people
I have below, Student.java -> contains method to add Students in the database addStudent(int
I'm creating an addStudent method and it looks like this: package gui; import java.awt.*;
The standard way to avoid package name conflicts in Java is to use reverse-domain

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.