0

i have a query built up by a criteriaset running in payara 5. My problem is that the query always translates the enum i have in my query to an ordinal even though the Type is annotated with @Enumerated(EnumType.STRING).

I logged all queries on the DB to be sure and see indeed the ordinal instead of the string there.

the enum looks like:

 public enum MyEnum { FIRST_OPTION, SECOND_OPTION, THIRD_OPTION, FOURTH_OPTION, ANY } 

the criteria builder function:

 public List<OptionHandler> get(MyEnum myEnum) { if (myEnum == null) { log.warning("No enum given!"); return List.of(); } final var builder = getEntityManager().getCriteriaBuilder(); final var query = builder.createQuery(OptionHandler.class); final var from = query.from(OptionHandler.class); query.select(from); final var match = builder.equal(from.get("myEnum"), myEnum); query.where(match); final var typedQuery = getEntityManager().createQuery(query); return typedQuery.getResultList(); } 

and the resultObject looks like this:

 public class OptionHandler { @NotNull @Enumerated(EnumType.STRING) private MyEnum myEnum; .... } 
2
  • 3
    This might be a stupid question, but did you make sure that you are using AccessType.FIELD and therefor JPA expects the annotation at the fields and not AccessType.PROPERTY that expects the annotations to be placed at the getters? Commented Sep 22, 2023 at 17:01
  • Check that you can read and persist your OptionHandler instances; is this a problem for all CRUD operations (suggesting the AccessType issue), or just a problem for this one criteria query? Commented Sep 22, 2023 at 17:07

1 Answer 1

0

The problem was indeed the accesstype. After annotating the getMethod instead of the field, it worked as intended

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.