Hibernate- failed to lazily initialize a collection of role: beans.Language.patients, could not initialize proxy - no Session
<p>I use hibernate to create a rest api. I create a method to get all items in a table.</p>

<pre><code>public List&lt;Language&gt; getAllLanguages(Session session) {
        List&lt;Language&gt; languages=(List&lt;Language&gt;)session.createQuery("from Language").list();
        return languages;
}
</code></pre>

<p>This is my Language.java</p>

<pre><code>public class Language  implements java.io.Serializable {


     private Integer idlanguage;
     private String language;
     private Set&lt;Patient&gt; patients = new HashSet&lt;Patient&gt;(0);

    public Language() {
    }


    public Language(String language) {
        this.language = language;
    }
    public Language(String language, Set&lt;Patient&gt; patients) {
       this.language = language;
       this.patients = patients;
    }

    public Integer getIdlanguage() {
        return this.idlanguage;
    }

    public void setIdlanguage(Integer idlanguage) {
        this.idlanguage = idlanguage;
    }
    public String getLanguage() {
        return this.language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
    public Set&lt;Patient&gt; getPatients() {
        return this.patients;
    }

    public void setPatients(Set&lt;Patient&gt; patients) {
        this.patients = patients;
    }

}
</code></pre>

<p>And this is my Patient.java</p>

<pre><code>// Generated Sep 14, 2016 4:33:23 PM by Hibernate Tools 4.3.1


import beans.DiabetesType;
import beans.Illness;
import beans.Language;
import beans.Reminder;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * Patient generated by hbm2java
 */
public class Patient  implements java.io.Serializable {

     private Integer idpatient;
     private DiabetesType diabetesType;
     private Language language;
     private String customId;
     private String diabetesOther;
     private String firstName;
     private String lastName;
     private String userName;
     private String password;
     private Date dateCreated;
     private Date lastUpdated;
     private Set&lt;Illness&gt; illnesses = new HashSet&lt;Illness&gt;(0);
     private Set&lt;Reminder&gt; reminders = new HashSet&lt;Reminder&gt;(0);


    public Patient() {
    }

    public Patient(Integer idpatient, String password) {
        this.idpatient = idpatient;
        this.password = password;
    }    

    public Patient(DiabetesType diabetesType, Language language, String customId, String firstName, String userName, String password, Date lastUpdated) {
        this.diabetesType = diabetesType;
        this.language = language;
        this.customId = customId;
        this.firstName = firstName;
        this.userName = userName;
        this.password = password;
        this.lastUpdated = lastUpdated;
    }
    public Patient(DiabetesType diabetesType, Language language, String customId, String diabetesOther, String firstName, String lastName,  String userName, String password, Date dateCreated, Date lastUpdated, Set&lt;Illness&gt; illnesses, Set&lt;Reminder&gt; reminders) {
       this.diabetesType = diabetesType;
       this.language = language;
       this.customId = customId;
       this.diabetesOther = diabetesOther;
       this.firstName = firstName;
       this.lastName = lastName;      
       this.userName = userName;
       this.password = password;
       this.dateCreated = dateCreated;
       this.lastUpdated = lastUpdated;
       this.illnesses = illnesses;
       this.reminders = reminders;
    }

    public Integer getIdpatient() {
        return this.idpatient;
    }

    public void setIdpatient(Integer idpatient) {
        this.idpatient = idpatient;
    }
    public DiabetesType getDiabetesType() {
        return this.diabetesType;
    }

    public void setDiabetesType(DiabetesType diabetesType) {
        this.diabetesType = diabetesType;
    }
    public Language getLanguage() {
        return this.language;
    }

    public void setLanguage(Language language) {
        this.language = language;
    }
    public String getCustomId() {
        return this.customId;
    }

    public void setCustomId(String customId) {
        this.customId = customId;
    }
    public String getDiabetesOther() {
        return this.diabetesOther;
    }

    public void setDiabetesOther(String diabetesOther) {
        this.diabetesOther = diabetesOther;
    }
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUserName() {
        return this.userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public Date getDateCreated() {
        return this.dateCreated;
    }

    public void setDateCreated(Date dateCreated) {
        this.dateCreated = dateCreated;
    }
    public Date getLastUpdated() {
        return this.lastUpdated;
    }

    public void setLastUpdated(Date lastUpdated) {
        this.lastUpdated = lastUpdated;
    }
    public Set&lt;Illness&gt; getIllnesses() {
        return this.illnesses;
    }

    public void setIllnesses(Set&lt;Illness&gt; illnesses) {
        this.illnesses = illnesses;
    }
    public Set&lt;Reminder&gt; getReminders() {
        return this.reminders;
    }

    public void setReminders(Set&lt;Reminder&gt; reminders) {
        this.reminders = reminders;
    }
}
</code></pre>

<p><strong>Important: The beans and mappings are reverse engineered from MySQL database, via NetBeans. I do not need to get any data related to <code>patient</code> when calling <code>getAllLangauges</code>. My <code>language</code> table has only 2 columns, <code>idlanguage</code> and <code>language</code>. <code>Patient</code> table has a foriegn key of <code>language table</code></strong></p>

<p>Before using this method in rest api , it worked perfectly without any exception. But when I used this in rest api, it created a complexity in there. </p>

<p>I am not using annotations in here. I used hibernate reverse engineering wizard to map above entities . This is my rest api method.</p>

<pre><code>@Path("/language")
public class LanguageJSONService {

    @GET
    @Path("/getAllLanguages")
    @Produces(MediaType.APPLICATION_JSON)
    public List&lt;Language&gt; getAllLanguages(){
        LanguageService languageService=new LanguageService();
        List&lt;Language&gt; list = languageService.getAllLanguages();
        return list;
    }
}
</code></pre>

<p>This is the way how I call the method,</p>

<pre><code>Client client = ClientBuilder.newClient();
List&lt;Language&gt; list = client.target("http://localhost:8080/simple_rest/rest")
                .path("/language/getAllLanguages")
                .request(MediaType.APPLICATION_JSON)
                .get(new GenericType&lt;List&lt;Language&gt;&gt;() {
                });

for (int i = 0; i &lt; list.size(); i++) {
      System.out.println("Id - " + list.get(i).getIdlanguage() + " Language - " + list.get(i).getLanguage());
}
</code></pre>

<p>When I call the method ,</p>

<pre><code>failed to lazily initialize a collection of role: beans.Language.patients, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]-&gt;beans.Language["patients"])
</code></pre>

<p>is occurred.</p>

<p>Interestingly, if I did not close the session, then I get an output like below which is totally something else, seems like it is trying to display its foreign key tables and their foreign key tables and so on...</p>

<pre><code>    [{"idlanguage":1,"language":"English","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":{"iddiabetesType":1,"type":"Sever","patients":
[{"idpatient":1,"diabetesType":
</code></pre>

<p>Have any ideas about this problem ? </p>

<p><strong>Update</strong></p>

<p>my configuration file </p>

<p>hibernate.cfg.xml</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
&lt;hibernate-configuration&gt;
  &lt;session-factory&gt;
    &lt;property name="show_sql"&gt;true&lt;/property&gt;
    &lt;property name="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
    &lt;property name="hibernate.connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
    &lt;property name="hibernate.connection.url"&gt;jdbc:mysql://localhost:3306/*****&lt;/property&gt;
    &lt;property name="hibernate.connection.username"&gt;*****&lt;/property&gt;
    &lt;property name="hibernate.c3p0.min_size"&gt;5&lt;/property&gt;
    &lt;property name="hibernate.c3p0.max_size"&gt;20&lt;/property&gt;
    &lt;property name="hibernate.c3p0.timeout"&gt;3000&lt;/property&gt;
    &lt;property name="hibernate.c3p0.max_statements"&gt;50&lt;/property&gt;
    &lt;property name="hibernate.c3p0.idle_test_period"&gt;300&lt;/property&gt;
    &lt;property name="hibernate.c3p0.testConnectionOnCheckout"&gt;true&lt;/property&gt;
    &lt;property name="hibernate.c3p0.preferredTestQuery"&gt;SELECT 1&lt;/property&gt;
    &lt;property name="hibernate.connection.password"&gt;************&lt;/property&gt;
    &lt;mapping resource="beans/Reminder.hbm.xml"/&gt;
    &lt;mapping resource="beans/Food.hbm.xml"/&gt;
    &lt;mapping resource="beans/Patient.hbm.xml"/&gt;
    &lt;mapping resource="beans/Illness.hbm.xml"/&gt;
    &lt;mapping resource="beans/Language.hbm.xml"/&gt;
  &lt;/session-factory&gt;
&lt;/hibernate-configuration&gt;
</code></pre>

<p>Language.hbm.xml</p>

<pre><code>&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
&lt;!-- Generated Sep 14, 2016 4:33:23 PM by Hibernate Tools 4.3.1 --&gt;
&lt;hibernate-mapping&gt;
    &lt;class name="beans.Language" table="language" catalog="myglukose" optimistic-lock="version"&gt;
        &lt;id name="idlanguage" type="java.lang.Integer"&gt;
            &lt;column name="idlanguage" /&gt;
            &lt;generator class="identity" /&gt;
        &lt;/id&gt;
        &lt;property name="language" type="string"&gt;
            &lt;column name="language" length="45" not-null="true" /&gt;
        &lt;/property&gt;
        &lt;set name="patients" table="patient" inverse="true" lazy="true" fetch="select"&gt;
            &lt;key&gt;
                &lt;column name="language_idlanguage" not-null="true" /&gt;
            &lt;/key&gt;
            &lt;one-to-many class="beans.Patient" /&gt;
        &lt;/set&gt;
    &lt;/class&gt;
&lt;/hibernate-mapping&gt;
</code></pre>

<p>This is my patient mapping file,</p>

<p>Patient.hbm.xml</p>

<pre><code>&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"&gt;
&lt;!-- Generated Sep 14, 2016 4:33:23 PM by Hibernate Tools 4.3.1 --&gt;
&lt;hibernate-mapping&gt;
    &lt;class name="beans.Patient" table="patient" catalog="myglukose" optimistic-lock="version"&gt;
        &lt;id name="idpatient" type="java.lang.Integer"&gt;
            &lt;column name="idpatient" /&gt;
            &lt;generator class="identity" /&gt;
        &lt;/id&gt;
        &lt;many-to-one name="diabetesType" class="beans.DiabetesType" fetch="select"&gt;
            &lt;column name="diabetes_type_iddiabetes_type" not-null="true" /&gt;
        &lt;/many-to-one&gt;
        &lt;many-to-one name="language" class="beans.Language" fetch="select"&gt;
            &lt;column name="language_idlanguage" not-null="true" /&gt;
        &lt;/many-to-one&gt;
        &lt;property name="customId" type="string"&gt;
            &lt;column name="custom_id" length="45" not-null="true" /&gt;
        &lt;/property&gt;
        &lt;property name="diabetesOther" type="string"&gt;
            &lt;column name="diabetes_other" length="45" /&gt;
        &lt;/property&gt;
        &lt;property name="firstName" type="string"&gt;
            &lt;column name="first_name" length="100" not-null="true" /&gt;
        &lt;/property&gt;
        &lt;property name="lastName" type="string"&gt;
            &lt;column name="last_name" length="100" /&gt;
        &lt;/property&gt;        
        &lt;property name="userName" type="string"&gt;
            &lt;column name="user_name" length="45" not-null="true" /&gt;
        &lt;/property&gt;
        &lt;property name="password" type="string"&gt;
            &lt;column name="password" length="45" not-null="true" /&gt;
        &lt;/property&gt;
        &lt;property name="dateCreated" type="timestamp"&gt;
            &lt;column name="date_created" length="19" /&gt;
        &lt;/property&gt;
        &lt;property name="lastUpdated" type="timestamp"&gt;
            &lt;column name="last_updated" length="19" not-null="true"&gt;
                &lt;comment&gt;Stores the basic information of the patient&lt;/comment&gt;
            &lt;/column&gt;
        &lt;/property&gt;
        &lt;set name="illnesses" table="illness" inverse="true" lazy="true" fetch="select"&gt;
            &lt;key&gt;
                &lt;column name="patient_idpatient" not-null="true" /&gt;
            &lt;/key&gt;
            &lt;one-to-many class="beans.Illness" /&gt;
        &lt;/set&gt;
        &lt;set name="reminders" table="reminder" inverse="true" lazy="true" fetch="select"&gt;
            &lt;key&gt;
                &lt;column name="patient_idpatient" not-null="true" /&gt;
            &lt;/key&gt;
            &lt;one-to-many class="beans.Reminder" /&gt;
        &lt;/set&gt;
    &lt;/class&gt;
&lt;/hibernate-mapping&gt;
</code></pre>