javahibernatelistcriteriarestrictions

How to avoid : " The expression of type List needs unchecked conversion to conform to.."


I have this code :

List<Book> bookList = session.createCriteria(Book.class)
                .add(Restrictions.like("name", "%i%")).list();

But, i have a notice which say : "Type safety: The expression of type List needs unchecked conversion to conform to List"

How can i, fix my code for remove this warning?


Solution

  • add this above the line or in top of the method header:

    @SuppressWarnings("unchecked")
    List<Book> bookList = session.createCriteria(Book.class)
                .add(Restrictions.like("name", "%i%")).list();
    

    or for the whole method:

    @SuppressWarnings("unchecked")
    public void doSomething(){
    

    in case list() would be your own implementation you could define the result like this:

    private List<Book> list(){
        return new ArrayList<Book>();
      }
    

    then the annotation is not necessary and you have a checked conversion controlled by the compiler.