Gabriel Bauman
Gabriel Bauman

Reputation: 2416

How should I refer to inner enums (defined within an entity) 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 state AWESOME using JPQL and Hibernate?

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

...gives the error...

org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'stuff.Thing.State.AWESOME'

Upvotes: 18

Views: 4001

Answers (2)

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

select t from Thing t where t.state='AWESOME'

Upvotes: 0

Gabriel Bauman
Gabriel Bauman

Reputation: 2416

Use the following idiom:

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

Type$InnerType is Java's naming convention for inner types.

When you try to use dot notation, Hibernate assumes that you're trying to access nested properties, which (properly) fails in this case.

Upvotes: 25

Related Questions