I am getting the infamous LazyInitializationException in a simple web app I am writing.
I have 2 entities being access: Person and Report
Here is the Person Java Class:
Code:
package dao.entity;
// Generated May 6, 2010 2:28:45 PM by Hibernate Tools 3.2.1.GA
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Person generated by hbm2java
*/
public class Person implements java.io.Serializable {
private int personId;
private String fullName;
private Date lastLogin;
private Set<Report> reports = new HashSet<Report>(0);
public Person() {
}
public Person(int personId) {
this.personId = personId;
}
public Person(int personId, String fullName, Date lastLogin, Set<Report> reports) {
this.personId = personId;
this.fullName = fullName;
this.lastLogin = lastLogin;
this.reports = reports;
}
public int getPersonId() {
return this.personId;
}
public void setPersonId(int personId) {
this.personId = personId;
}
public String getFullName() {
return this.fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public Date getLastLogin() {
return this.lastLogin;
}
public void setLastLogin(Date lastLogin) {
this.lastLogin = lastLogin;
}
public Set<Report> getReports() {
return this.reports;
}
public void setReports(Set<Report> reports) {
this.reports = reports;
}
}
And here is the Hibernate Mapping File:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 6, 2010 2:28:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="dao.entity.Person" table="PERSON" schema="busn0110ridPm1" lazy="false" >
<id name="personId" type="int">
<column name="PERSON_ID" precision="9" scale="0" />
<generator class="increment" />
</id>
<property name="fullName" type="string">
<column name="FULL_NAME" length="300" />
</property>
<property name="lastLogin" type="date">
<column name="LAST_LOGIN" length="7" />
</property>
<set name="reports" inverse="true">
<key>
<column name="PERSON_ID" precision="9" scale="0" />
</key>
<one-to-many class="dwd.dao.entity.Report" />
</set>
</class>
</hibernate-mapping>
Here is the Report Java Class:
Code:
package dao.entity;
// Generated May 6, 2010 2:28:45 PM by Hibernate Tools 3.2.1.GA
import java.util.Date;
/**
* Report generated by hbm2java
*/
public class Report implements java.io.Serializable {
private int reportId;
private Project project;
private Person person;
private Date dateSubmitted;
private String pageUrl;
private String message;
private String status;
private String expectedResults;
private String developer_notes;
public Report() {
}
public Report(int reportId) {
this.reportId = reportId;
}
public Report(int reportId, Project project, Person person, Date dateSubmitted, String pageUrl, String message, String status) {
this.reportId = reportId;
this.project = project;
this.person = person;
this.dateSubmitted = dateSubmitted;
this.pageUrl = pageUrl;
this.message = message;
this.status = status;
}
public String getDeveloper_notes() {
return developer_notes;
}
public void setDeveloper_notes(String developer_notes) {
this.developer_notes = developer_notes;
}
public String getExpectedResults() {
return expectedResults;
}
public void setExpectedResults(String expectedResults) {
this.expectedResults = expectedResults;
}
public int getReportId() {
return this.reportId;
}
public void setReportId(int reportId) {
this.reportId = reportId;
}
public Project getProject() {
return this.project;
}
public void setProject(Project project) {
this.project = project;
}
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
public Date getDateSubmitted() {
return this.dateSubmitted;
}
public void setDateSubmitted(Date dateSubmitted) {
this.dateSubmitted = dateSubmitted;
}
public String getPageUrl() {
return this.pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public String getStatus() {
return this.status;
}
public void setStatus(String status) {
this.status = status;
}
}
And the Report Hibernate Mapping File:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 6, 2010 2:28:51 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="dao.entity.Report" table="REPORT" schema="busn0110ridPm1" lazy="false">
<id name="reportId" type="int">
<column name="REPORT_ID" precision="9" scale="0" />
<generator class="increment" />
</id>
<many-to-one name="project" class="dwd.dao.entity.Project" fetch="select">
<column name="PROJECT_ID" precision="9" scale="0" />
</many-to-one>
<many-to-one name="person" class="dwd.dao.entity.Person" fetch="select">
<column name="PERSON_ID" precision="9" scale="0" />
</many-to-one>
<property name="dateSubmitted" type="date">
<column name="DATE_SUBMITTED" length="7" />
</property>
<property name="pageUrl" type="string">
<column name="PAGE_URL" length="1000" />
</property>
<property name="message" type="string">
<column name="MESSAGE" length="0" />
</property>
<property name="status" type="string">
<column name="STATUS" length="500" />
</property>
<property name="expectedResults" type="string">
<column name="EXPECTED_RESULTS" length="4000" />
</property>
<property name="developer_notes" type="string">
<column name="DEVELOPER_NOTES" length="4000" />
</property>
</class>
</hibernate-mapping>
I have a Data Access Object set up to handle my Hibernate functions. Here are the portions of the file that I am using for this particular feature:
Code:
...
public ArrayList getReportsByProjectId(int project_id) {
String hql = "from Report where project_id=" + project_id;
return getObjects(hql);
}
public ArrayList getObjects(String hql) {
ArrayList list = new ArrayList();
try {
session = getSession();
trans = session.beginTransaction();
list = (ArrayList) session.createQuery(hql).list();
trans.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeSession(session);
}
return list;
}
...
In my page controller I grab the reports by the project_id and loop through them to display on the page. When I attempt to access the Person data it fails and gives me the LazyInitializationException.
From this information can anybody help me understand the proper HQL or Java syntax that populates the Person within the Report records I am retrieving? I guess I assumed that Hibernate automatically fetched that data from the Person table when I did the call for the reports. After all I am getting the person_id out of the Person object...
Any help with this would be greatly appreciated.
Thanks