Question

This results in unchecked warning:

public List<Person> list()
{
  return sessionFactory.getCurrentSession().createQuery( "FROM Person" ).list();
}

Already tried, without success:

public List<Person> list()
{
  Query query = sessionFactory.getCurrentSession().createQuery( "FROM Person" );
  return Collections.checkedList( query.list(), Person.class );
}

I do NOT want to add

@SuppressWarnings( "unchecked" )

because it wouldn't fix it...

So, how to have type safety?

Was it helpful?

Solution

At some place a conversion from the untyped list to the typed list has to happen. This conversion is unsafe. Collections.checkedList would allow you to move the annotation but not more; not very helpful here. The annotation does not "solve" the problem in a technical sense, it just tells the compiler that you know what you do. As the Query interface does not return typed results (for good reason) there is no way around either a warning or the SuppressWarnings annotation.

You should set the annotation and write a meaningful comment why it is justified (i.e. because the type is determined by the HQL query but the interface returns an untyped list).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top