Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPQL: Enum literal in SELECT NEW query

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:

  String jpql = "SELECT NEW model.ModelDescriptor"
    + "(t.id, t.name, t.description, :modelType) ... ";
  TypedQuery<ModelDescriptor> query = em.createQuery(jpql, ModelDescriptor.class);
  query.setParameter("modelType", ModelType.forClass(clazz));
  List<ModelDescriptor> list = query.getResultList();

This does not work. No exception is thrown but the type is null in the results. Also it is not possible to pass the enum literal to the query:

  "SELECT NEW model.ModelDescriptor (f.id, f.name, f.description,   
    model.ModelType.FLOW) ... "

edit I get the following stack trace:

  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)

I use EclipseLink as persistence framework.

Is there a way to pass an enum literal into an SELECT NEW expression?

like image 436
Michael Avatar asked Dec 14 '11 11:12

Michael


People also ask

Which of the following methods is used to execute a select JPQL query?

Query createQuery(String name) - The createQuery() method of EntityManager interface is used to create an instance of Query interface for executing JPQL statement.

Is Hql and JPQL same?

The Hibernate Query Language (HQL) and Java Persistence Query Language (JPQL) are both object model focused query languages similar in nature to SQL. JPQL is a heavily-inspired-by subset of HQL. A JPQL query is always a valid HQL query, the reverse is not true however.

What do you understand by JPQL query?

The Jakarta Persistence Query Language (JPQL; formerly Java Persistence Query Language) is a platform-independent object-oriented query language defined as part of the Jakarta Persistence (JPA; formerly Java Persistence API) specification. JPQL is used to make queries against entities stored in a relational database.

Which is JPQL aggregate function?

JPQL supports the five aggregate functions of SQL: COUNT - returns a long value representing the number of elements. SUM - returns the sum of numeric values. AVG - returns the average of numeric values as a double value.


1 Answers

No there is not, in general there is no way to reference to the fields in any class and it is also not possible to pass argument to the SELECT clause. Only valid arguments to the constructor expression are (from JPA 2.0 specification, page 174)

  • single_valued_path_expression
  • scalar_expression
  • aggregate_expression
  • identification_variable
like image 155
Mikko Maunu Avatar answered Oct 11 '22 21:10

Mikko Maunu