I have this method where I cast the results to (List ) , but my eclipse is still complaining ! Type safety: Unchecked cast from List to List
@Override
public List<String> getDevices(Long productId) {
String queryString = "SELECT op.name FROM t_operation op WHERE op.discriminator = 'ANDROID' and PRODUCT =:productId ";
try {
Query query = getEntityManager().createQuery(queryString);
query.setParameter("productId", productId);
return (List<String> ) query.getResultList();
} catch (RuntimeException re) {
throw re;
}
}
If we can't eliminate the “unchecked cast” warning and we're sure that the code provoking the warning is typesafe, we can suppress the warning using the SuppressWarnings(“unchecked”) annotation. When we use the @SuppressWarning(“unchecked”) annotation, we should always put it on the smallest scope possible.
An unchecked warning tells a programmer that a cast may cause a program to throw an exception somewhere else. Suppressing the warning with @SuppressWarnings("unchecked") tells the compiler that the programmer believes the code to be safe and won't cause unexpected exceptions.
You can sure use TypedQuery with the parameter types is String in this case. So what you need is
TypedQuery<String> query = getEntityManager().createQuery(queryString, String.class);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With