Java、Hibernate java.lang.ClassCastException:org.hibernate.collection.PersistentSet 无法转换为 java.util.HashSet
我有两张表,DVD 和 Contact。
可以将 DVD 租给联系人,并且联系人可以租用多张 DVD。
多对一链接(dvd-->contact)
工作正常。
但另一种方法失败了: (contact-->dvd)
这是联系人映射:
<set name="dvds" inverse="true">
<key column="contactId"/>
<one-to-many class="Dvd"/>
</set>
这是联系人的 setter getter:
private Set<Dvd> dvds = new HashSet<Dvd>();
public Set<Dvd> getDvds(){
return dvds;
}
public void setDvds(Set<Dvd> dvds){
this.dvds=dvds;
}
当我尝试从联系人那里租借 DVD 时:
HashSet<Dvd> tt = (HashSet<Dvd>)dds;
我得到异常:
java.lang.ClassCastException: org.hibernate.collection.PersistentSet
cannot be cast to java.util.HashSet
异常是什么意思以及如何修复它?
编辑:这解决了我的问题:
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
I have two tables, DVD and Contact.
A DVD can be rented to a contact and a contact can rent many DVD's.
The many to one link (dvd-->contact)
works fine.
But the other way fails: (contact-->dvd)
This is the contact mapping:
<set name="dvds" inverse="true">
<key column="contactId"/>
<one-to-many class="Dvd"/>
</set>
Here is setter getter for Contact:
private Set<Dvd> dvds = new HashSet<Dvd>();
public Set<Dvd> getDvds(){
return dvds;
}
public void setDvds(Set<Dvd> dvds){
this.dvds=dvds;
}
When I try to get the DVD rented from a contact with this:
HashSet<Dvd> tt = (HashSet<Dvd>)dds;
I get an Exception:
java.lang.ClassCastException: org.hibernate.collection.PersistentSet
cannot be cast to java.util.HashSet
What does the Exception mean and how do I fix it?
Edit: This solved my problem:
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不需要转换为
HashSet
。它是一个Set
,并且不提供任何额外的方法。所以就不要投射。这是使用集合时的一般规则 - 不要用它们的具体类来引用它们(除非您确实需要)。使用
List
和Set
,而不是ArrayList
和HashSet
You don't need to cast to
HashSet
. It is aSet
, and it does not provide any additional methods. So just don't cast.This is a general rule when working with collections - don't refer to them with their concrete classes (unless you really need to). Use
List
andSet
, rather thanArrayList
andHashSet
不要尝试将
Set
dds
转换为HashSet
。 Hibernate 使用自己的Set
接口实现,称为PersistentSet
,它不是从HashSet
派生的,因此转换会抛出ClassCastException.通过
Set
接口使用它,或者使用其构造函数创建一个新的HashSet
(在这种情况下,您对集合的更改将不会自动反映在 Hibernate 中)。或者
Don't try to cast the
Set
dds
intoHashSet
. Hibernate uses its own implementation of theSet
interface calledPersistentSet
which does not derive fromHashSet
and hence the casting throws aClassCastException
. Either use it through theSet
interface, or create a newHashSet
using its constructor (in which case your changes to the set will not be reflected in Hibernate automatically).OR
我最近遇到了这个问题。
我能够消除选角问题。
列表<对象> listObject = Arrays.asList(ListFromHibernate.toArray());
然后你可以通过转换 List 中的对象来获取对象。
MyObject x = (MyObject) listObject.get(1);
PS:现在是可怕的 2013 年。
I've come across this problem recently.
I was able to eradicate the casting problem.
List<Object> listObject = Arrays.asList(ListFromHibernate.toArray());
Then you can get the objects by casting the objects in List, say.
MyObject x = (MyObject) listObject.get(1);
PS: It's freaking 2013.
Abhinav Sarkar的答案当然是正确的,但也有你的建模错误。
DVD 和联系人之间的关系是多对多,而不是多对一(否则每张 DVD 对于一个客户来说都是唯一的)
Abhinav Sarkar's answer is correct of course, but there is also a mistake in your modeling.
The relationship between DVD and Contact is Many-To-Many, not Many-To-One (otherwise each DVD would be unique to one single customer)