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

gaymer:

我有两个桌子,DVD和Contact。

可以将DVD租借给联系人,而联系人可以租用许多DVD。

多对一链接(dvd-->contact)可以正常工作。

但是另一种方法失败了: (contact-->dvd)

这是联系人映射:

<set name="dvds" inverse="true">
   <key column="contactId"/>
   <one-to-many class="Dvd"/>
</set>

这是联系人的设置器获取器:

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)
博zh:

您无需强制转换为HashSet它是Set,并且不提供任何其他方法。所以只是不要投。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章