简体   繁体   中英

How to wrapper Hibernate session API avoid of “unchecked” generics warning

I'm coding a wrapper for 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. 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.

Since List is a raw type you'll have to do an unchecked conversion somewhere . The best you can do is to keep it contained to one place, which is what you're doing with execQuery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM