最近遇到一个奇怪的问题,我在实体类的基类中使用了org.apache.commons.lang.builder.HashCodeBuilder,然后在one-to-many关系映射(customer <---> order)中出现org.hibernate.LazyInitializationException: illegal access to loading collection异常。
小弟刚刚接触hibernate,这个问题找了两天才发现问题所在,但是仍然不知道为什么会这样,请大家不吝赐教。
环境:struts + hibernate3, hibernate2也试验过
异常:
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:353)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:327)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:194)
at cz.model.BaseObject.hashCode(BaseObject.java:78)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:392)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:353)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:327)
at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:194)
at cz.model.BaseObject.hashCode(BaseObject.java:78)
at java.util.HashMap.hash(HashMap.java:261)...
Hibernate 3 和 2 都是这个异常
只是2的异常描述是:net.sf.hibernate.LazyInitializationException: cannot access loading collection
程序片断:
BO基类 cz.model.BaseObject
abstract public class BaseObject implements Serializable {
public BaseObject(){}
...
public String toString() {
return ToStringBuilder.reflectionToString(this,
ToStringStyle.MULTI_LINE_STYLE);
}
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}
//注释掉这个函数就不出现异常
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
cz.model.Customer继承BaseObject
public class Customer extends BaseObject implements Serializable {
...
}
cz.model.Order继承BaseObject
public class Order extends BaseObject implements Serializable {
...
}
映射文件
Customer.hbm.xml
<?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="cz.model.Customer" table="CUSTOMERS">
<id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
...
<set
name="orders"
cascade="all-delete-orphan"
inverse="true"
lazy="true">
<key column="CUSTOMER_ID" />
<one-to-many class="cz.model.Order"/>
</set>
</class>
</hibernate-mapping>
Order.hbm.xml
<hibernate-mapping>
<class name="cz.model.Order" table="ORDERS">
<id name="id" type="long" column="ID">
<generator class="increment" />
</id>
....
<many-to-one name="customer" column="CUSTOMER_ID"
class="cz.model.Customer" cascade="save-update"/>
</class>
</hibernate-mapping>
DAO实现类中的方法
public Customer getCustomerById(Long customerId) throws AppException {
Session session = HibernateUtil.getSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Query query = session.createQuery("from Customer c where c.id=:id");
query.setLong("id", customerId.longValue());
Customer customer = (Customer)query.uniqueResult();
Set orderset = customer.getOrders();
//在这里会抛出上面说的异常,如果在BaseObject.java中去掉
//public int hashCode() 方法就会正常,这是为什么呢??
System.out.println("orderset size=" + orderset.size());
tx.commit();
return customer;
} catch (Exception ex) {
HibernateUtil.rollbackTransaction(tx);
AppException.appError(ex).printStackTrace();
throw AppException.appError(ex);
} finally {
HibernateUtil.closeSession(session);
}
}
请大家帮我解答一下这个问题,谢谢。