pramod
pramod

Reputation:

warning:[unchecked] unchecked conversion

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 be 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();
    }...
.....
..}

Upvotes: 9

Views: 10006

Answers (6)

Rakesh Juyal
Rakesh Juyal

Reputation:

Probably the result 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:

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

Upvotes: 1

sheep
sheep

Reputation: 111

I am using EJB 3.1 and I had the same problem. I found another solution:

TypedQuery<espressobar_milk> query = manager.createQuery("SELECT m FROM espressobar_milk m", espressobar_milk.class);         
return query.getResultList(); 

This solves the unchecked conversation warning (you don't need the @SuppressWarnings annotation anymore).

Upvotes: 11

Douglas Harris
Douglas Harris

Reputation: 11

@SuppressWarning ( "unchecked" ) 

should be:

@SuppressWarnings("unchecked") 

Upvotes: 1

Schildmeijer
Schildmeijer

Reputation: 20946

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.

Upvotes: 3

Uri
Uri

Reputation: 89749

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.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500725

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.

Upvotes: 10

Related Questions