Hi All
I am running into this issue while i was just trying to get an object from db and display it on jsp. Object which i am fetching from db contains a many-to-one mapping for which i am getting this exception. It was very strange for me to have this exception as every thing seemed to me in place. I have also used spring's OpenSessionInViewFilter but still not able to solve the problem. I know one solution is to set lazy-initialization to false in mapping file but i don't want to do it because of performance reasons.
I am posting the code also . Please look at it and suggest some way out. I am using struts 2, Hibernate and spring as technology stack.
Person.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="attendance.model.Person" table="person">
<id column="id" name="id" type="long" unsaved-value="null">
<generator class="native" />
</id>
<property name="firstName" column="firstName" type="string" not-null="true" />
<property name="lastName" column="lastName" type="string" />
<property name="address" column="address" type="string" />
<many-to-one name="user" column="user" class="attendance.model.User" not-null="true" cascade="all" />
</class>
</hibernate-mapping>
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Attendance</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml, /WEB-INF/spring/*.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Person.java
Code:
/**
*
*/
package attendance.model;
import attendance.model.support.PersistentObject;
/**
* @author balkrishan
*
*/
public class Person extends PersistentObject {
private String firstName;
private String lastName;
private String address;
private User user;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
User.java
Code:
/**
*
*/
package attendance.model;
import attendance.model.support.PersistentObject;
/**
* @author balkrishan
*
*/
public class User extends PersistentObject {
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
jsp on which trying to use object
Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Person List</title>
</head>
<body>
<s:iterator value="personList" var="person">
<div>
First Name: ${person.firstName}
User Name: ${person.user.userName}
</div>
</s:iterator>
</body>
</html>
Do let me know if i missed to post some piece of code which you might want to look at
Thanks in advance.