Using Enum type in NamedQuery: Field is not found in type

#1

I'm trying to switch an application that worked fine in Hibernate to ObjectDB and am having issues with queries that reference enum types. On my entity object is a static enum

public static enum Type {
    A,B,C
}

that is stored in an @Enumerated member variable

private Type type;

@Enumerated
public Type getType() {
    return type;
}

I then try to query this field in the following JPA Named Query

@NamedQuery(name = "TestQuery",
                query = "select obj from Obj where obj.type = com.my.fully.qualified.name.Obj.Type.A")

but i get an ObjectDB UserException

com.objectdb.o.UserException: Field 'Type' is not found in type 'com.my.fully.qualified.name.Obj'

I have noticed that my entities seem not to be getting picked up as entities until they are first persisted - Hibernate used to scan my @Entity classes and create tables before the application even started whereas my ObjectDB database file isnt being created until the first EntityManager flush, so maybe that has something to do with it.

I've tried changing the @Enumerated to String type without success. Thats how I had it working with Hibernate - with just the quoted name string of the enum type in the query. ObjectDB didnt like that.

As far as I can tell, I'm doing everything correct according to the JPA Persistable Types page

Thanks for the help

#2

The problem may be related to using an inner class enum.

Please try:

SELECT o FROM Obj o WHERE o.type = com.my.fully.qualified.name.Obj$Type.A

i.e. $ instead of . to represent the inner class name.

The following example should work with an empty database:

import java.util.*;

import javax.persistence.*;


public final class T1009 {

    public static void main(String[] args)  {
        EntityManagerFactory emf =
                Persistence.createEntityManagerFactory(
                        "objectdb:$objectdb/db/test.tmp;drop");
        EntityManager em = emf.createEntityManager();
       
        em.getMetamodel().entity(MyEntity.class);

        Query query = em.createQuery("SELECT e FROM MyEntity e " +
            "WHERE e.type = " + Type.class.getName() + ".T1");
        System.out.println(query);
        List resultList = query.getResultList();
        System.out.println(resultList);

        em.close();
        emf.close();
    }
   
    @Entity
    public static class MyEntity {
        Type type;
    }

    enum Type { T1, T2 }
}

This should probably be fixed in future versions.

ObjectDB Support

Reply