Eclipse 警告:类型安全(Java 泛型)

发布于 2024-12-02 08:00:18 字数 1900 浏览 0 评论 0 原文

我有以下 Hibernate 代码:

List<Book> result;    

result = hibernateTemplate.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("SELECT DISTINCT b FROM Book as b LEFT JOIN FETCH b.authors");

            List list = query.list();

            return list;
        }
    });

我收到以下警告:从 hibernateTemplate.execute(...:

Multiple markers at this line
    - HibernateCallback is a raw type. References to generic type HibernateCallback<T> should be parameterized
    - Type safety: The expression of type new HibernateCallback(){} needs unchecked conversion to conform to HibernateCallback<Object>
    - Type safety: Unchecked invocation execute(new HibernateCallback(){}) of the generic method execute(HibernateCallback<T>) of type 
     HibernateTemplate
    - Type safety: The expression of type new HibernateCallback(){} needs unchecked conversion to conform to HibernateCallback<List<Book>>
    - Type safety: The expression of type List needs unchecked conversion to conform to List<Book>

所以这真的很难。

请您解释一下

  1. 编译器看到什么与什么它期望什么,为什么?

  2. 修复这些警告的最安全方法是什么...即不是通过@SuppressWarnings("unchecked")

?已尝试以下链接中出现的提案: https://forums.oracle.com/forums/thread.jspa?threadID=1182661 (页面底部出现的三个建议中的第二个)......但是它不起作用。

谢谢!

PS我确实知道如何解决由于 List list = query.list(); 而应该收到的其他警告,这就是为什么我没有在问题中提及它。

PS-2 根据Eclipse,该方法的签名是;对象 org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateCallback action) 抛出 DataAccessException

I have the following Hibernate code:

List<Book> result;    

result = hibernateTemplate.execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery("SELECT DISTINCT b FROM Book as b LEFT JOIN FETCH b.authors");

            List list = query.list();

            return list;
        }
    });

I am getting the following warnings starting at hibernateTemplate.execute(...:

Multiple markers at this line
    - HibernateCallback is a raw type. References to generic type HibernateCallback<T> should be parameterized
    - Type safety: The expression of type new HibernateCallback(){} needs unchecked conversion to conform to HibernateCallback<Object>
    - Type safety: Unchecked invocation execute(new HibernateCallback(){}) of the generic method execute(HibernateCallback<T>) of type 
     HibernateTemplate
    - Type safety: The expression of type new HibernateCallback(){} needs unchecked conversion to conform to HibernateCallback<List<Book>>
    - Type safety: The expression of type List needs unchecked conversion to conform to List<Book>

So this is really tough.

Could you please explain

  1. What does the compiler see versus what it expects, and why?

  2. What is the safest way to fix these warnings... i.e. not by @SuppressWarnings("unchecked") ?

I have tried the proposal appearing in the following link:
https://forums.oracle.com/forums/thread.jspa?threadID=1182661
(the second suggestion out of the three appearing at the bottom of the page).... however it did not work.

Thanks!

P.S. I do know how to solve the other warning I am supposed to get due to the List list = query.list();, this is why I am not mentioning it in my question.

P.S-2 According to Eclipse, the signature of the method is <Object> Object org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateCallback<Object> action) throws DataAccessException

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

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

发布评论

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

评论(2

兰花执着 2024-12-09 08:00:18

该类的签名是 HibernateCallback 并定义了一个方法 T doInHibernate(Session) 但您不提供类型参数T——这就是编译器抱怨的地方:不确定你的代码是否真的产生了适合你的结果的List变量。

您是正确的,添加 @SuppressWarnings 不是一个好主意(它实际上并没有增加类型安全性),请尝试这样做:

List<Book> result = hibernateTemplate.execute(new HibernateCallback<List<Book>>() {
    public List<Book> doInHibernate(Session session) throws HibernateException, SQLException {
        Query query = session.createQuery("SELECT DISTINCT b FROM Book as b LEFT JOIN FETCH b.authors");

        List list = query.list();

        return list;
    }
});

这将类型参数 T 设置为您的预期结果类型为 List,特别是这意味着:

  • new HibernateCallback>() 而不仅仅是 new HibernateCallback()
  • 然后要求 public Object doInHibernate(Session ... 变为 public List doInHibernate(Session ...)

这仍然留下了未参数化的对象List list = query.list(); 您可以通过 如何使用 Hibernate HQL 结果避免类型安全警告? (我个人更喜欢那里提到的演员助手方法)。

The signature of that class is HibernateCallback<T> and defines a method T doInHibernate(Session) but you don't supply the type parameter T -- that's what the compiler is complaining about: It's not sure you're your code is actually resulting in a List<Book> that fits into your result variable.

You are correct in that adding @SuppressWarnings isn't a good idea (it doesn't increase actually type safety), try this instead:

List<Book> result = hibernateTemplate.execute(new HibernateCallback<List<Book>>() {
    public List<Book> doInHibernate(Session session) throws HibernateException, SQLException {
        Query query = session.createQuery("SELECT DISTINCT b FROM Book as b LEFT JOIN FETCH b.authors");

        List list = query.list();

        return list;
    }
});

This sets the type parameter T to your expected result type of List<Book>, in particular this means:

  • new HibernateCallback<List<Book>>() instead of just new HibernateCallback()
  • this then demands public Object doInHibernate(Session ... to become public List<Book> doInHibernate(Session ...

This still leaves the unparameterized List list = query.list(); which you could handle in one of the ways describe in How to avoid type safety warnings with Hibernate HQL results? (I'd personally prefer the cast-helper approach mentioned there).

寄离 2024-12-09 08:00:18

是不是……

new HibernateCallback<List>()...

这可能意味着方法返回类型将是 List 而不是 Object。那么您还需要指定 List 类型吗?

Is it not just ...

new HibernateCallback<List>()...

Which presumably means the method return type would be a List rather than Object. Though then you'd need to specify the List type too?

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