How to use Enum as NamedQuery parameters in JPA

n002213f picture n002213f · May 12, 2010 · Viewed 9.3k times · Source

I have an Entity with a enum attribute and a couple on NamedQueries. One of these NamedQueries has the enum attribute as a parameter i.e.

SELECT m FROM Message m WHERE m.status = :status

When i try to ru n the query i get the following error;

Caused by: java.lang.IllegalArgumentException: You have attempted to set a value of type class my.package.Status for parameter status with expected type of class my.package.Status from query string SELECT m FROM Message m WHERE m.status = :status.

I'm using Toplink

How is this? How would i make JPA happy?

Answer

Bozhidar Batsov picture Bozhidar Batsov · May 12, 2010

Enum objects can be used as query parameters just like any other objects. You query however should probably be:

SELECT m FROM Message m WHERE m.status = :status

Also - have added the @Enumerated in the entity definition?