[求教]为什么我在使用HashCodeBuilder在one-to-many关系中出现Lazy异常

最近遇到一个奇怪的问题,我在实体类的基类中使用了org.apache.commons.lang.builder.HashCodeBuilder,然后在one-to-many关系映射(customer <---> order)中出现org.hibernate.LazyInitializationException: illegal access to loading collection异常。

小弟刚刚接触hibernate,这个问题找了两天才发现问题所在,但是仍然不知道为什么会这样,请大家不吝赐教。

[b]环境:[/b]struts + hibernate3, hibernate2也试验过
[b]异常: [/b]
[b]org.hibernate.LazyInitializationException: illegal access to loading collection[/b]
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)
[b]at cz.model.BaseObject.hashCode(BaseObject.java:78)[/b]
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

[b]程序片断:
BO基类 cz.model.BaseObject [/b]

[code]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);
}[/code]
[b]cz.model.Customer继承BaseObject [/b]
[code]public class Customer extends BaseObject implements Serializable {
...
}[/code]
[b]cz.model.Order继承BaseObject [/b]
[code]public class Order extends BaseObject implements Serializable {
...
}[/code]
[b]映射文件[/b]

[b]Customer.hbm.xml[/b]
[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="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>[/code]
[b]Order.hbm.xml[/b]
[code]<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>[/code]
[b]DAO实现类中的方法[/b]
[code] 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);
}
}[/code]

请大家帮我解答一下这个问题,谢谢。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值