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>
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
What does the compiler see versus what it expects, and why?
What is the safest way to fix these warnings... i.e. not by @SuppressWarnings("unchecked") ?
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
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 ...)
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).
发布评论
评论(2)
该类的签名是
HibernateCallback
并定义了一个方法T doInHibernate(Session)
但您不提供类型参数T
——这就是编译器抱怨的地方:不确定你的代码是否真的产生了适合你的结果的
List
变量。您是正确的,添加
@SuppressWarnings
不是一个好主意(它实际上并没有增加类型安全性),请尝试这样做:这将类型参数
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 methodT doInHibernate(Session)
but you don't supply the type parameterT
-- that's what the compiler is complaining about: It's not sure you're your code is actually resulting in aList<Book>
that fits into yourresult
variable.You are correct in that adding
@SuppressWarnings
isn't a good idea (it doesn't increase actually type safety), try this instead:This sets the type parameter
T
to your expected result type ofList<Book>
, in particular this means:new HibernateCallback<List<Book>>()
instead of justnew HibernateCallback()
public Object doInHibernate(Session ...
to becomepublic 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).是不是……
这可能意味着方法返回类型将是 List 而不是 Object。那么您还需要指定 List 类型吗?
Is it not just ...
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?