How can I reference internal enums (defined inside an object) from a JPQL query using Hibernate?

I have an entity class as follows:

package stuff; @Entity class Thing { @Id @GeneratedValue private Long id; @Basic @Enumerated private State state; public enum State { AWESOME, LAME } } 

How can I select all things with AWESOME state using JPQL and Hibernate?

 select t from Thing t where t.state=stuff.Thing.State.AWESOME 

... gives an error ...

 org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'stuff.Thing.State.AWESOME' 
+8
java enums hibernate jpa jpql
source share
2 answers

Use the following idiom:

 select t from Thing t where t.state=stuff.Thing$State.AWESOME 

Type$InnerType - Java assignment convention for internal types.

When you try to use dot notation, Hibernate assumes that you are trying to access nested properties that (properly) do not execute in this case.

+10
source share
 select t from Thing t where t.state='AWESOME' 
0
source share

All Articles