什么是“正确的”?将Hibernate Query.list()转换为List< Type&gt ;?的方法? [英] What is the "proper" way to cast Hibernate Query.list() to List<Type>?

查看:244
本文介绍了什么是“正确的”?将Hibernate Query.list()转换为List< Type&gt ;?的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,我正在编写一个简单的方法来返回匹配特定过滤器的对象列表
List< Foo> 似乎是一种自然的返回类型。

I'm a newbie with Hibernate, and I'm writing a simple method to return a list of objects matching a specific filter. List<Foo> seemed a natural return type.

无论我做什么,我似乎都无法使编译器很开心,除非我使用丑陋 @SuppressWarnings

Whatever I do, I can't seem to make the compiler happy, unless I employ an ugly @SuppressWarnings.

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

public class Foo {

    public Session acquireSession() {
        // All DB opening, connection etc. removed,
        // since the problem is in compilation, not at runtime.
        return null;
    }

    @SuppressWarnings("unchecked") /* <----- */

    public List<Foo> activeObjects() {
        Session s = acquireSession();
        Query   q = s.createQuery("from foo where active");
        return (List<Foo>) q.list();
    }
}

我想摆脱那个 SuppressWarnings 即可。但是,如果我这样做,我会收到警告

I would like to get rid of that SuppressWarnings. But if I do, I get the warning

Warning: Unchecked cast from List to List<Foo>

(我可以忽略它,但是我想首先得到它),如果我删除通用符合 .list()返回类型,我得到警告

(I can ignore it, but I'd like to not get it in the first place), and if I remove the generic to conform to .list() return type, I get the warning

Warning: List is a raw type. References to generic type List<E>
should be parameterized.

我注意到 org.hibernate.mapping 声明一个 List ;但它完全是另一种类型 - Query 返回一个 java.util.List 作为原始类型。我觉得很奇怪,最近的Hibernate(4.0.x)不会实现参数化类型,所以我怀疑它是我而不是做错了什么。

I noticed that org.hibernate.mapping does declare a List; but it is a different type altogether - Query returns a java.util.List, as a raw type. I find it odd that a recent Hibernate (4.0.x) would not implement parameterized types, so I suspect that it's me instead doing something wrong.

它看起来很像将Hibernate结果转换为对象列表,但在这里我没有硬错误(系统知道类型Foo,并且我没有使用SQLQuery,而是直接查询)。所以没有快乐。

It looks very much like Cast Hibernate result to a list of objects, but here I have no "hard" errors (the system knows type Foo, and I'm not using a SQLQuery but a straight Query). So no joy.

我也看过 Hibernate类转换异常,因为它看起来很有希望,但后来我意识到我做的不是实际上得到任何 Exception ...我的问题只是一个警告 - 一种编码风格,如果你愿意的话。

I have also looked at Hibernate Class Cast Exception since it looked promising, but then I realized that I do not actually get any Exception... my problem is just that of a warning - a coding style, if you will.

有关jboss.org,Hibernate手册和一些教程的文档似乎没有涵盖<这样的细节(或者我没有在正确的地方搜索?)。当他们详细介绍时,他们会使用即时演员 - 而这些演讲不在官方的jboss.org网站上,所以我有点谨慎。

Documentation on jboss.org, Hibernate manuals and several tutorials do not seem to cover the topic in such detail (or I didn't search in the right places?). When they do enter into detail, they use on-the-fly casting - and this on tutorials that weren't on the official jboss.org site, so I'm a bit wary.

代码一旦编译完成,运行时没有明显的问题......但我知道...;结果是预期的结果。

The code, once compiled, runs with no apparent problem... that I know of... yet; and the results are the expected ones.

所以:我是这样做的吗?我错过了明显的东西吗?是否有官方
或建议

So: am I doing this right? Am I missing something obvious? Is there an "official" or "recommended" Way To Do It?

推荐答案

简短回答 @SuppressWarnings 是正确的选择。

Short answer @SuppressWarnings is the right way to go.

长时间回答,Hibernate返回一个原始<$ c从 Query.list 方法的$ c> List ,参见here 的。这不是Hibernate的一个错误或者可以解决的问题,查询返回的类型在编译时是未知的

Long answer, Hibernate returns a raw List from the Query.list method, see here. This is not a bug with Hibernate or something the can be solved, the type returned by the query is not known at compile time.

因此当你写

Therefore when you write

final List<MyObject> list = query.list();

您正在从 List List< MyObject> - 这是无法避免的。

You are doing an unsafe cast from List to List<MyObject> - this cannot be avoided.

没有办法安全地执行演员因为 List could 可以包含任何内容。

There is no way you can safely carry out the cast as the List could contain anything.

使错误消失的唯一方法是离开是更加丑陋的

The only way to make the error go away is the even more ugly

final List<MyObject> list = new LinkedList<>();
for(final Object o : query.list()) {
    list.add((MyObject)o);
}

这篇关于什么是“正确的”?将Hibernate Query.list()转换为List&lt; Type&gt ;?的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆