Fast Object Database for Java - with JPA/JDO support

JPA JPQL WHERE clause for IN :variable not working if variable is a list of Enums



I have a case where I have a JPQL query like:

"select o from MyEntity o WHERE (enumField IN :enumFieldList)"

And enum field is defined in MyEntity as:

    @Column(nullable = false)
    private MyEnumType enumField;

And MyEnumType is defined as:

    public enum MyEnumType { VALUE1, VALUE2, VALUE3, VALUE4 }

and I call:

        Arrays.asList(MyEnumType.VALUE1, MyEnumType.VALUE2));

Then query.getResultSet() *always* returns an empty list.

BUT if (for example) I try "WHERE (enumField = : enumField1) OR (enumField = :enumField2)" with:

    query.setParameter("enumField1", MyEnumType.VALUE1);
    query.setParameter("enumField2", MyEnumType.VALUE2);

Then the matching objects are returned.


I understood the JPA JPQL IN clause should work with Enum types - is that the case, or is this an objectdb bug ?




Just tried the following code and it seems to work:

package test;

import java.util.*;
import javax.persistence.*;

public class T1254 {

    public static void main(String[] args) {
        EntityManagerFactory emf =
        EntityManager em = emf.createEntityManager();

        em.persist(new MyEntity(MyEnumType.VALUE1));
        em.persist(new MyEntity(MyEnumType.VALUE2));

        Query query = em.createQuery(
            "SELECT o FROM MyEntity o WHERE (enumField IN :enumFieldList)");
            Arrays.asList(MyEnumType.VALUE1, MyEnumType.VALUE3));

    public static class MyEntity {
        MyEntity(MyEnumType enumField) {
            this.enumField = enumField;
        MyEnumType enumField;
        public String toString() {
            return String.valueOf(enumField);
    public enum MyEnumType { VALUE1, VALUE2, VALUE3, VALUE4 }

Please try this example, and if necessary adjust it to demonstrate the issue that you had.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

I managed to reproduce the issue and found the @Enumerated(EnumType.STRING) annotation is making the difference. If the annotation is present, the problem described by the op occurs. If it is missing everything works fine as stated in the first reply.


Indeed using @Enumerated(EnumType.STRING) in the example of #2 above demonstrates the issue.

It happened because ObjectDB considered a collection of emum values as a query parameter argument to be always @Enumerated(EnumType.ORDINAL). This is fixed now in build 2.6.1_05.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply