Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Including enum in JPA query

I have a class in the following form.

@Entity
public class Person{
    public enum SEX {
      MALE, FEMALE, OTHER
    }

    private String name;
    private SEX sex;
 }

And I have an interface class which extends the JpaRepostory. The enum doesn't work in the query. I am trying to using Spring JPA for fetchig the data.

public interface PersonRepository extends JpaRepository<Person, Long> {


    @Query("SELECT p FROM Person p WHERE 
            "p.SEX = com.example.Person.Sex.MALE " +
            "AND p.name = :name")
    public List<Person> checkName(@Param("name") String name,);

}

I get the following exception

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'com.example.Person.Sex.MALE'

How can I fix it ?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You're not respecting the case of your classes and attributes. It should be

p.sex = com.example.Person.SEX.MALE
about 4 years ago · Santiago Trujillo Report

0

Beyond the case errors which have already been mentioned, there is another problem: your enum class is a nested class. And a third problem is that Hibernate seems to have problems with capitalized class names, i.e. you need to change the class name from SEX to Sex.

Variant 1:

Define the enum as a top-level class in package com.example

package com.example;
public enum Sex {
    MALE, FEMALE, OTHER
}

and then you can access an enum value from the repository using e.g. com.example.Sex.MALE:

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query("SELECT p FROM Person p WHERE " + 
            "p.sex = com.example.Sex.MALE " +
            "AND p.name = :name")
    public List<Person> checkName(@Param("name") String name);

}

Surprisingly (a bug?) this will end up with an error if you change the name of the enum from Sex to SEX (while adjusting the name in the query obviously).

Variant 2:

You could keep the definition of Sex as a nested class. The enum is then compiled into the class with the name Person$Sex.class. Simply use this name in the query string:

@Entity
public class Person {

    public enum Sex {
        MALE, FEMALE, OTHER
    }

    @Id @GeneratedValue long id;
    private String name;
    private Sex sex;
    
    public Person() {}
    public Person(String name, Sex sex) {
        this.name = name;
        this.sex = sex;
    }
}

The repository then needs to be written as

public interface PersonRepository extends JpaRepository<Person, Long> {

    @Query("SELECT p FROM Person p WHERE " + 
            "p.sex = com.example.Person$Sex.MALE " +
            "AND p.name = :name")
    public List<Person> checkName(@Param("name") String name);

}

Once again, if you change the name of the nested class fom Sex to SEX (also in the query string) then an exception with the text

Invalid path: 'com.example.Person$SEX.MALE' 
   [SELECT p FROM com.example.Person p WHERE p.sex = com.example.Person$SEX.MALE 
                                                             AND p.name = :name]

is thrown, although the generated byte code is stored in class Person$SEX.class.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!