休眠-> ArrayList 无法转换为 Set

发布于 2024-09-26 18:48:43 字数 692 浏览 1 评论 0原文

我有一个 Java EE 应用程序,并且使用 Hibernate。域对象,我将List / ArrayList更改为Set / HashSet,因为使用Sets更好。

但是在我的 Dao 实现中我遇到了一个问题:

public Set<Person> getAllPersons() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session sess = sessionFactory.getCurrentSession();

    Transaction tx = sess.beginTransaction();
    @SuppressWarnings("unchecked")
    Set<Item> items = (Set<Item>) sess.createQuery("from Item").list();
    tx.commit();

    return items;
}

这里我得到一个错误:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set

我该怎么做才能避免这个错误?

预先感谢您&此致。

I have a Java EE application and I use Hibernate. The domain objects, I changed the List / ArrayList to Set / HashSet, because it is better to use Sets.

But in my Dao implementation I run into a problem:

public Set<Person> getAllPersons() {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session sess = sessionFactory.getCurrentSession();

    Transaction tx = sess.beginTransaction();
    @SuppressWarnings("unchecked")
    Set<Item> items = (Set<Item>) sess.createQuery("from Item").list();
    tx.commit();

    return items;
}

Here I get an error:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set

What can I do to avoid this error?

Thank you in advance & Best Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

烟柳画桥 2024-10-03 18:48:43
List<Item> list = sess.createQuery("from Item").list();
Set<Item> items = new HashSet<Item>(list);  

sess.createQuery("from Item").list(); 将返回结果项的数组列表,如果您在 Set 中需要它,可以按照代码所示进行制作

List<Item> list = sess.createQuery("from Item").list();
Set<Item> items = new HashSet<Item>(list);  

sess.createQuery("from Item").list(); will return a array list of resulting items, if you need it in Set you can make it as shown in code

假情假意假温柔 2024-10-03 18:48:43

领域对象,我把List/ArrayList改成了Set/HashSet,因为用Sets比较好。

使用 Set 并不是“更好”,它完全取决于您的需要。但无论如何,您在域对象中使用的集合类型以及 Query#list()两个不相关的事物

就我个人而言,我不能说我认为将查询结果转换为 Set 有多大价值(除了占用更多内存),除非您明确想要删除 当然是查询结果的重复项(但在这种情况下,我会质疑映射)。

现在,如果您坚持的话,各种 Set 实现都有一个采用 Collection 的构造函数(并且问题本质上是 将列表转换为集合的最简单方法? - Java)。

The domain objects, I changed the List / ArrayList to Set / HashSet, because it is better to use Sets.

It's not "better" to use Set, it depends entirely on what you need. But anyway, the collection type you use in your domain objects and the return type of Query#list() are two unrelated things.

Personally, I can't say that I see much value in converting the result of a query into a Set (apart from eating more memory), unless you explicitly want to remove duplicates from query results of course (but in that case, I would challenge the mappings).

Now, if you insist, the various Set implementations have a constructor that takes a Collection (and the question is essentially a duplicate of Easiest way to convert a List to a Set? - Java).

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