Java、Hibernate java.lang.ClassCastException:org.hibernate.collection.PersistentSet 无法转换为 java.util.HashSet

发布于 2024-10-10 02:53:00 字数 952 浏览 0 评论 0原文

我有两张表,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遮云壑 2024-10-17 02:53:00

您不需要转换为HashSet。它是一个Set,并且不提供任何额外的方法。所以就不要投射。

这是使用集合时的一般规则 - 不要用它们的具体类来引用它们(除非您确实需要)。使用 ListSet,而不是 ArrayListHashSet

You don't need to cast to HashSet. It is a Set, 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 and Set, rather than ArrayList and HashSet

归属感 2024-10-17 02:53:00

不要尝试将 Set dds 转换为 HashSet。 Hibernate 使用自己的 Set 接口实现,称为 PersistentSet,它不是从 HashSet 派生的,因此转换会抛出 ClassCastException.通过 Set 接口使用它,或者使用其构造函数创建一个新的 HashSet(在这种情况下,您对集合的更改将不会自动反映在 Hibernate 中)。

Set<Dvd> tt = dds;

或者

HashSet<Dvd> tt = new HashSet<Dvd>(dds);

Don't try to cast the Set dds into HashSet. Hibernate uses its own implementation of the Set interface called PersistentSet which does not derive from HashSet and hence the casting throws a ClassCastException. Either use it through the Set interface, or create a new HashSet using its constructor (in which case your changes to the set will not be reflected in Hibernate automatically).

Set<Dvd> tt = dds;

OR

HashSet<Dvd> tt = new HashSet<Dvd>(dds);
初雪 2024-10-17 02:53:00

我最近遇到了这个问题。
我能够消除选角问题。

列表<对象> 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.

空宴 2024-10-17 02:53:00

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文