Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type safety: Unchecked cast from List to List<String>

Tags:

java

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;
        }

    }
like image 260
María Belén Esteban Menéndez Avatar asked Jun 27 '16 09:06

María Belén Esteban Menéndez


People also ask

How do I fix unchecked unchecked cast?

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.

What are unchecked warnings in Java?

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.


1 Answers

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); 
like image 126
Lê Thọ Avatar answered Sep 23 '22 13:09

Lê Thọ