ObjectDB ObjectDB

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

#1

Hi,

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:

    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    @Index
    private MyEnumType enumField;

And MyEnumType is defined as:

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

and I call:

    query.setParameter("enumFieldList",
        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 ?

 

Thanks

edit
delete
#2

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 =
            Persistence.createEntityManagerFactory(
                "objectdb:test.tmp;drop");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        em.persist(new MyEntity(MyEnumType.VALUE1));
        em.persist(new MyEntity(MyEnumType.VALUE2));
        em.getTransaction().commit();

        Query query = em.createQuery(
            "SELECT o FROM MyEntity o WHERE (enumField IN :enumFieldList)");
        query.setParameter("enumFieldList",
            Arrays.asList(MyEnumType.VALUE1, MyEnumType.VALUE3));
        System.out.println(query.getResultList());
       
        em.close();
        emf.close();
    }

    @Entity
    public static class MyEntity {
        MyEntity(MyEnumType enumField) {
            this.enumField = enumField;
        }
        MyEnumType enumField;
        @Override
        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
edit
delete
#3

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.

edit
delete
#4

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
edit
delete

Reply

To post on this website please sign in.