JPQL: list literal in SELECT NEW query

I have a descriptor class for a pair of domain classes. The descriptor class has a type field, which is an enumeration and indicates the type of the domain class. In some queries, I want to return to or more descriptors and pass the type as an argument to the constructor. 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 was thrown, but type null in the results. It is also not possible to pass an enumeration literal to a 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 the basis of conservation.

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

+4
source share
1 answer

No, no, in general, there is no way to refer to fields in any class, and it is also impossible to pass an argument to a SELECT clause. Only valid arguments in the constructor expression (from the JPA 2.0 specification, p. 174)

  • single_valued_path_expression
  • scalar_expression
  • aggregate_expression
  • identification_variable
+4
source

Source: https://habr.com/ru/post/1386307/


All Articles