我有一个父/子@OneToMany
,其中集合是Set
(没有重复)。如果我尝试从服务器上的父级访问子级,我会收到以下错误。
如果我通过将 Parent 传递给客户端request.setAttribute
并使用 JSTL 访问 Parent,我可以直接访问 Child 对象成员。父/子有 getter/Setter。
我正在使用 Tomcat 6.0.32、Spring 3.1.0、JDK6、Hibernate 3。
家长
@Entity
@Table(name="SUSER")
public class SUser {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int su_id;
@Column(name="displayname", nullable=true,
columnDefinition="varchar", length=50, insertable=true, updatable=true)
private String displayname;
@Column(name="last_activity", nullable=true, columnDefinition="datetime",
insertable=true, updatable=true)
private String last_activity;
@Column(name="ldapuser", nullable=true, columnDefinition="varchar",
length=50, insertable=true, updatable=true)
private String ldapuser;
@OneToMany
@org.hibernate.annotations.IndexColumn(name="SU_ID")
@JoinColumn(name="su_id", insertable=true, updatable=true)
private Set<SUserAttributes> suattr = new HashSet<SUserAttributes>();
孩子
@Entity
@Table(name="SUSER_ATTRIBUTES")
public class SUserAttributes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int sua_id;
@Column(name="su_id")
private int su_id;
@Column(name="sua_key", nullable=true, columnDefinition="varchar",
length=64, insertable=true, updatable=true)
private String sua_key;
@Column(name="sua_value", nullable=true, columnDefinition="varchar",
length=128, insertable=true, updatable=true)
private String sua_value;
道
package com.oasis.implementation.dao;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.oasis.implementation.ClientInfo;
import com.oasis.implementation.SUser;
public class SUserDaoImpl implements SUserDao {
@SuppressWarnings("unused")
private static Logger logger = Logger
.getLogger(ClientDownloadsDaoImpl.class);
@Autowired
@Qualifier("implementationSessionFactory")
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public List<SUser> getAllSUsers(){
List<SUser> suList = null;
Session session = null;
try
{
session = sessionFactory.openSession();
session.beginTransaction();
suList = session.createQuery("from SUser").list();
session.getTransaction().commit();
}
catch (RuntimeException e)
{
System.out.println (e.getMessage());
}
finally
{
if (session != null)
{
session.close();
}
}
return suList;
}
@Override
public List<SUser> getSUserById(int su_id) {
Session session = null;
SUser sUser = null;
LinkedList<SUser> suList = new LinkedList<SUser>();
try {
session = sessionFactory.openSession();
session.beginTransaction();
sUser = (SUser) session.get(SUser.class, su_id);
suList.add(sUser);
session.getTransaction().commit();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
} finally {
if (session != null) {
session.close();
}
}
return suList;
}
@Override
public List<SUser> getUserByLdap(String ldapuser){
Session session = null;
int user_id = 0;
List<SUser> suList = null;
try
{
session = sessionFactory.openSession();
session.beginTransaction();
SQLQuery sqlq = session.createSQLQuery("select su_id from
suser where ldapuser = '" + ldapuser + "'");
user_id = (Integer)sqlq.uniqueResult();
suList = getSUserById(user_id);
session.getTransaction().commit();
}
catch (RuntimeException e)
{
System.out.println (e.getMessage());
}
finally
{
if (session != null)
{
session.close();
}
}
return suList;
}
}
错误信息
Hibernate: select suser0_.su_id as su1_11_0_, suser0_.displayname as
displayn2_11_0_, suser0_.last_activity as last3_11_0_, suser0_.ldapuser as l
dapuser11_0_ from SUSER suser0_ where suser0_.su_id=?
ERROR - failed to lazily initialize a collection of role: com.oasis.implementation.SUser.suattr, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.oasis.implementation.SUser.suattr, no session or
session was closed
at