Search code examples
javajpajava-ee-6criteria-api

java criteriaset always using ordinal


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;

    ....
 
    }

Solution

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