Note that there are some explanatory texts on larger screens.

plurals
  1. POJPQL: Enum literal in SELECT NEW query
    primarykey
    data
    text
    <p>I have a descriptor class for a couple of domain classes. The descriptor class has a field 'type' which is an enum and indicates the type of the domain class. In some queries I want to return on or more descriptors and pass the type as constructor argument. So my idea was to pass it as a query parameter:</p> <pre><code> String jpql = "SELECT NEW model.ModelDescriptor" + "(t.id, t.name, t.description, :modelType) ... "; TypedQuery&lt;ModelDescriptor&gt; query = em.createQuery(jpql, ModelDescriptor.class); query.setParameter("modelType", ModelType.forClass(clazz)); List&lt;ModelDescriptor&gt; list = query.getResultList(); </code></pre> <p>This does not work. No exception is thrown but the type is <code>null</code> in the results. Also it is not possible to pass the enum literal to the query:</p> <pre><code> "SELECT NEW model.ModelDescriptor (f.id, f.name, f.description, model.ModelType.FLOW) ... " </code></pre> <p><strong>edit</strong> I get the following stack trace:</p> <pre><code> java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: Exception Description: Error compiling the query [SELECT model.ModelDescriptor(f.id, f.name, f.description, model.ModelType.FLOW) FROM Flow f WHERE flow.id = :flowId], line 1, column 78: unknown identification variable [model]. The FROM clause of the query does not declare an identification variable [model]. at org.eclipse.persistence.internal.jpa.EntityManagerImpl. createQuery(EntityManagerImpl.java:1477) at org.eclipse.persistence.internal.jpa.EntityManagerImpl. createQuery(EntityManagerImpl.java:1497) </code></pre> <p>I use EclipseLink as persistence framework.</p> <p>Is there a way to pass an enum literal into an SELECT NEW expression?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload