Hi,
I have an issue while mapping many-to-many or one-to-many collection using non unique column of one table with the other.(Code of Tables,java and hbm files are mentioned below).
In jsp when I try accessing the collection(example department.employees.size()) its throwing below exception.
When lazy is true for the collection set in hbmCode:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.entity.Department.employees, no session or session was closed
When lazy is false for the collection set in hbmCode:
org.hibernate.HibernateException: collection is not associated with any session
at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
I am even using the org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor in the dispatcher setting my singleSession to true and flushModeName to FLUSH_AUTO.
I tried searching in the Internet but haven't got any solution for last three days. I would really appreciate if you can provide a solution.
Regards,
Ravi
The tables are
Department
Code:
CREATE TABLE DEPARTMENT
(
ID VARCHAR2(30 BYTE),NAME VARCHAR2(30 BYTE),NON_UNIQUE_ID VARCHAR2(30 BYTE)
)
Employee
Code:
CREATE TABLE EMPLOYEE
(
ID VARCHAR2(30 BYTE),NAME VARCHAR2(30 BYTE)
)
Department_employee_rel
Code:
CREATE TABLE DEPARTMENT_EMPLOYEE_REL
(
NON_UNIQUE_ID VARCHAR2(30 BYTE),
EMPLOYEE_ID VARCHAR2(30 BYTE)
)
The java entities are as below
Department
Code:
package com.entity;
import java.io.Serializable;
import java.util.Set;
public class Department implements Serializable {
static final long serialVersionUID = -1L; private String id; private String name; private String nonUniqueId;
private Set<Employee> employees;
public Set<Employee> getEmployees() {
return employees;
}
public void setEmployees(Set<Employee> employees) {
this.employees = employees;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNonUniqueId() {
return nonUniqueId;
}
public void setNonUniqueId(String nonUniqueId) {
this.nonUniqueId= nonUniqueId;
}
}
Employee
Code:
package com.entity;
import java.io.Serializable;
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String id; private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
The department hbm file contains the below code
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="none" default-lazy="true" package="com.entity">
<class name="Department" table="DEPARTMENT" >
<cache usage="transactional" />
<id name="id" column="ID" />
<property name="nonUniqueId" column="NON_UNIQUE_ID" />
<property name="name" column="NAME" />
<set name="employees" table="department_employee_rel">
<key column="department_id" property-ref="nonUniqueId"/>
<many-to-many column="employee_id" class="Employee" unique="true" />
</set>
</class>
</hibernate-mapping>
Employee hbm
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-cascade="none" default-lazy="true" package="com.entity">
<class name="Employee" table="employee" >
<cache usage="transactional" />
<id name="id" column="ID" />
<property name="name" column="NAME" />
</class>
</hibernate-mapping>