Java JPA "Error compiling the query" when it uses an enum -


the following jpa query doesn't compile:

@namedquery(name = "psa.findbysourcesystem",      query = "select p psa p p.sourcesystem.id = :sourcesystemid") 

p.sourcesystem following enum:

public enum sourcesystem {     first(3, "abc"), second(9, "def"), third(17, "ghi");      private int id;     private string code;     ... } 

and mapped in psa's base class:

public class psabase implements serializable {     @column(name = "sourcesystemid")     @enumerated(enumtype.ordinal)     protected sourcesystem sourcesystem;     ... } 

the query compiles , runs fine if replace p.sourcesystem.id in query more benign.

thank in advance help.

it shouldn't compile.

you have resolve required enum value manually before passing query parameter:

@namedquery(name = "psa.findbysourcesystem",       query = "select p psa p p.sourcesystem = :sourcesystem")  

.

public enum sourcesystem {      ...      private static map<integer, sourcesystem> valuesbyid = new hashmap<integer, sourcesystem>();     static {         (sourcesystem s: values())              valuesbyid.put(s.id, s);     }     public static sourcesystem findbyid(int id) {         return valuesbyid.get(id);     } }  

.

em.createnamedquery("psa.findbysourcesystem")     .setparameter("sourcesystem", sourcesystem.findbyid(sourcesystemid)); 

edit: since sourcesystem annotated @enumerated(enumtype.ordinal), it's stored in database ordinal numbers of corresponding enum values, therefore first stored 0. jpa doesn't directly support using arbitrary field of enum value identify in database. if database schema assumes so, can following trick decouple state of object database schema:

public class psabase implements serializable {      protected sourcesystem sourcesystem;       @column(name = "sourcesystemid")      public integer getsourcesystemid() {         return sourcesystem.getid();     }      public void setsourcesystemid(integer id) {         this.sourcesystem = sourcesystem.findbyid(id);     }     ... getter , setter of sourcesystem @transient ... }  

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -