views:

590

answers:

4

Hi all,

I am getting following warning :

warning:[unchecked] unchecked conversion
[javac]found:java.util.List
[javac] required:java.util.List<edu.fullerton.cs476s09.espressobar.jpa.espressobar_milk>
return query.getResultList();

What may the problem and probable solution. I am using following code:

@Stateless
@Remote(Order.class)
//@EntityListeners(MyListener.class)
public class OrderBean implements Order
{ 
    /**
     * The entity manager object, injected by the container
     */

    @PersistenceContext

    private EntityManager manager;

    public List<espressobar_milk> listMilk() 
    {
        Query query = manager.createQuery("SELECT m FROM espressobar_milk m");
        return query.getResultList();
    }...
.....
..}

Thanks in advance for any suggestion.

+3  A: 

Well, we don't have details of what Query is here, but presumably its getResultList method returns a raw List. Basically the compiler can't be sure that the list returned by getResultList will only contain instances of espressobar_milk (it's slightly more subtle than that, but we'll leave it there for now).

You could try to make it strongly typed, probably by changing the Query class, or you could annotate the method with

@SuppressWarnings("unchecked")

if you're convinced it's correct but there's no way of achieving compile-time safety.

Jon Skeet
Note, I still received a warning when I annotated the method in Java 1.7. However, the warning was actually suppressed when I annotated the entire class.
Chris S
+1  A: 

Probably the resultant of

return query.getResultList();

is List and not List< E >, as it is expected ( List< espressobar_milk > ). [ http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html ] That's the reason it is showing the warning. In any case you can suppress this warning using

@SuppressWarning ( "unchecked" )
public List<espressobar_milk> listMilk() 
{
    Query query = manager.createQuery("SELECT m FROM espressobar_milk m");
    return query.getResultList();
}
+1  A: 

Unchecked conversion warnings typically result from Java's Type Erasure mechanism for Generics. The idea is that code that uses generics can cooperate with code that does not use generics (in fact, the generated code omits the generic type).

However,you also get this warning when you try to "tack on" genericity to a non generic list (since this is different than a class-based cast that would throw a ClassCastException). In this case, trying to tack on the milk type to what is a non-generic list return by getResultList produces the error.

Read the Java Generics tutorial for more info on this.

Uri
+2  A: 

Looks like you are using Hibernate, which is currently implementing JPA 1.0 (Hopefully Hibernate will implement JPA 2.0 when it is released. (Toplink is currently the reference implementation of JPA 2.0)). Hibernate is not using any java 1.5 features and thus no generic collections are available.

Schildmeijer