[英]How to wrapper Hibernate session API avoid of “unchecked” generics warning
I'm coding a wrapper for Hibernate Session API: 我正在为Hibernate Session API编写包装器:
@SuppressWarnings("unchecked")
public <T> List<T> execQuery(String hql){
Session s=sf.getCurrentSession();
return s.createQuery(hql)
.list();
}
The list() function of Hibernate Session API seems using raw type. Hibernate Session API的list()函数似乎使用原始类型。 so How can I wrapper this function, and let the client of my wrapper to safyly using like this:
因此,我该如何包装该函数,并让包装器的客户端安全地使用如下所示:
List<BusinessObject> bo= xxx.execQuery(...);
There's no way to avoid having @SuppressWarnings("unchecked")
in at least one place. 无法避免在至少一个地方使用
@SuppressWarnings("unchecked")
。
Since List
is a raw type you'll have to do an unchecked conversion somewhere . 由于
List
是原始类型,因此您必须在某处进行未经检查的转换。 The best you can do is to keep it contained to one place, which is what you're doing with execQuery. 最好的办法是将其包含在一个地方,这就是您使用execQuery所做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.