java 获取“非法访问加载集合”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7860064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting "illegal access to loading collection" error
提问by Coder_sLaY
When I execute my program without implementing hashcode()
and toString()
then it works fine. But as soon as I include hashcode()
and toString()
then I get this "illegal access to loading collection" error.
当我在没有执行的情况下执行我的程序时hashcode()
,toString()
它工作正常。但只要我有hashcode()
和toString()
,然后我得到这个错误“来装载收集非法访问”。
My hbm files are
我的 hbm 文件是
1) booking.hbm.xml
1) 预订.hbm.xml
<many-to-one name="userId" class="User" column="user_id"
insert="true" update="true" cascade="save-update" >
</many-to-one>
<many-to-one name="flightId" class="FlightSchedule"
column="flight_id" cascade="all" not-null="true">
</many-to-one>
<set name="passenger" table="passenger79215" lazy="false"
inverse="true" cascade="save-update">
<key column="reference_id" />
<one-to-many class="Passenger" />
</set>
2) Passenger.hbm.xml
2) 乘客.hbm.xml
<many-to-one name="referenceid" class="Booking" lazy="false"
insert="true" update="true" column="reference_id "
cascade="save-update">
</many-to-one>
3) User.hbm.xml
3) 用户.hbm.xml
<set name="booking" table="bookings79215" lazy="true"
inverse="false" cascade="save-update">
<key column="user_id" />
<one-to-many class="Booking" />
</set>
Can anyone explain the error?
任何人都可以解释错误吗?
采纳答案by Ravi Bhatt
Your hashcode and equals methods are not working properly. Make sure that they are correct. toString()
has nothing to do with collection classes but hashcode and equals does.
您的 hashcode 和 equals 方法无法正常工作。确保它们是正确的。toString()
与集合类无关,但 hashcode 和 equals 有关系。
I assume that you have overridden both hashcode and equals and not only hashcode.
我假设您已经覆盖了哈希码和等于,而不仅仅是哈希码。
回答by extraneon
I think you should not use the id field (managed by hibernate) in equals and/or hashCode.
我认为您不应该在 equals 和/或 hashCode 中使用 id 字段(由 hibernate 管理)。
Equals and hashCode should be implemented as a business logic equals.
Equals 和 hashCode 应该实现为业务逻辑 equals。
回答by DeegC
I had the same error but with a different resolution. Like the OP I'm using Apache's hashcode builder. My objects are Parent and Child with a one-to-many relationship. The child includes Parent as a member so that the foreign key gets set properly.
我有同样的错误,但分辨率不同。就像 OP 一样,我使用的是 Apache 的哈希码构建器。我的对象是具有一对多关系的父子对象。子项包括 Parent 作为成员,以便正确设置外键。
The problem is that the hashcode builder uses all the member fields but when Child is being created its Parent hasn't finished loading yet. When the hashcode builder references the Parent member Hibernate throws the exception because Parent is still loading.
问题是哈希码构建器使用了所有成员字段,但是在创建 Child 时,其 Parent 尚未完成加载。当哈希码构建器引用 Parent 成员时,Hibernate 会抛出异常,因为 Parent 仍在加载。
The fix was to exclude the parent reference from the hashcode builder in Child's hashCode and equals:
解决方法是从 Child 的 hashCode 和 equals 中的 hashcode 构建器中排除父引用:
@Override
public boolean equals(final Object obj)
{
return EqualsBuilder.reflectionEquals(this, obj, "parent" );
}
@Override
public int hashCode()
{
return HashCodeBuilder.reflectionHashCode(this, "parent" );
}