Java – Avoid type safety warnings with Hibernate criteria query

genericshibernatejava

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

results in this :
Type safety: The expression of type List needs unchecked conversion to conform to List

Is their a method to remove the warning, as I get an error using this :

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

Best Answer

Well, you can use:

@SuppressWarnings("unchecked")

before the declaration...

Note that this will only suppress the warning - it won't do anything to make the code safer. In this case, I'd personally be happy enough about this; I'd trust Hibernate to do the right thing.