我有几个域类的描述符类.描述符类有一个字段’type’,它是一个枚举,表示域类的类型.在某些查询中,我想返回或更多描述符,并将类型作为构造函数参数传递.所以我的想法是将它作为查询参数传递:
String jpql = "SELECT NEW model.ModelDescriptor"
+ "(t.id, t.name, t.description, :modelType) ... ";
TypedQuery query = em.createQuery(jpql, ModelDescriptor.class);
query.setParameter("modelType", ModelType.forClass(clazz));
List list = query.getResultList();
这不起作用.不会抛出异常,但结果中的类型为null.此外,无法将枚举文字传递给查询:
"SELECT NEW model.ModelDescriptor (f.id, f.name, f.description,
model.ModelType.FLOW) ... "
编辑
我得到以下堆栈跟踪:
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)
我使用EclipseLink作为持久性框架.
有没有办法将枚举文字传递给SELECT NEW表达式?