Avoid type safety warnings asking for Hibernate criteria

final Criteria crit = session.createCriteria(MyClass.class);
final List<MyClass> myClassList = crit.list();

leads to the following: Security type: an expression of type List requires an raw conversion to match List

Is their method for removing the warning as I get the error message:

final List<MyClass> myClassList = Collections.checkedList(MyClass.class, crit.list());
+5
source share
3 answers

Well, you can use:

@SuppressWarnings("unchecked")

before the announcement ...

Please note that this only suppresses the warning - it will do nothing to make the code more secure. In this case, I personally would be quite pleased about this; I would trust Hibernate to do the right thing.

+6
source

In your code

final List<MyClass> myClassList 
    = Collections.checkedList(MyClass.class, crit.list());

you just need to reverse the order of the arguments on checkedList().

Btw , . , checkedList() - -!

: checkedList() , - , , .

checkedList() , , . , . , ( ).

Jon Skeet (@SuppressWarnings("unchecked")) .

+4

- Matt Quail : Hibernate HQL?, , Query, :

@SuppressWarnings("unchecked")
public static <T> List<T> listAndCast(Criteria crit) {
  List<T> list = crit.list();
  return list;
}

And only you have to call this method:

List<MyClass> myClassList = listAndCast(crit);

Hello!

+1
source

All Articles