如何解决关联实体和非关联实体之间的连接,导致在 JPQL 中使用 Lazy fetch 生成非持久性实体不断抛出 JpqlSyntaxException
这个查询不断给我不同的错误 我怀疑我对构建 JPQL 查询的理解。
请帮我找出问题所在。
我试图在直接关联的实体和间接关联的实体之间进行连接,并在非持久性 DTO 中获取结果。
public List leaveRequestsReport(List employeesGroupIds) {
if(employeesGroupIds!=null && employeesGroupIds.size()==0)
employeesGroupIds =null;
return persistence.getEntityManager().
createquery("select new com.company.vp.workflow.LeaveRequestRow(at.requestState.request.requestNumber,"
+ "e.employeeNumber,e.fullName,at.requestState.request.submissionDate,e.employeesGroup.name,at.requestState.request.states) " + " from vp$RequestStateAttribute at "
+ " INNER JOIN at.requestState INNER JOIN at.requestState.request,"
+ " vp$Employee e INNER JOIN e.employeesGroup "
+ " where at.requestState.request.subjectType = 10 "
+ " and at.requestState.request.requestKind = 20 " )
.getResultList();
}
我得到的异常
com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException: Errors found for input JPQL:[select new com.company.vp.workflow.LeaveRequestRow(at.requestState.request.requestNumber,e.employeeNumber,at.requestState.request.states) from vp$RequestStateAttribute at INNER JOIN at.requestState INNER JOIN at.requestState.request,vp$Employee e INNER JOIN e.employeesGroup where at.requestState.request.subjectType = 10 and at.requestState.request.requestKind = 20]
No variable name found [Join variable: null]
No variable name found [Join variable: null]
No variable name found [Join variable: null]
at com.haulmont.cuba.core.global.QueryParserAstBased.getTree(QueryParserAstBased.java:76) ~[cuba-global-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.global.QueryParserAstBased.getAnalyzer(QueryParserAstBased.java:84) ~[cuba-global-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.global.QueryParserAstBased.getEntityName(QueryParserAstBased.java:101) ~[cuba-global-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.sys.QueryImpl.transformQueryString(QueryImpl.java:247) ~[cuba-core-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.sys.QueryImpl.getQuery(QueryImpl.java:134) ~[cuba-core-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.sys.QueryImpl.getResultList(QueryImpl.java:409) ~[cuba-core-7.2.6.jar:7.2.6]
实体如下 // 映射是 100% 正常工作
@Table(name = "VP_REQUEST_STATE")
@Entity(name = "vp$RequestState")
public class RequestState extends BaseUuidEntity implements AttributeHolder {
private static final long serialVersionUID = 6052950589685024134L; @OnDeleteInverse(DeletePolicy.CASCADE)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "REQUEST_ID")
protected Request request;
@Composition
@OnetoMany(mappedBy = "requestState")
protected List requestStateAttributes;
// setters and getters go here
}
@Table(name = "VP_REQUEST_STATE_ATTRIBUTE")
@Entity(name = "vp$RequestStateAttribute")
public class RequestStateAttribute extends BaseUuidEntity implements Creatable,ValuedAttributable {
private static final long serialVersionUID = 7406123321282514785L;
@OnDeleteInverse(DeletePolicy.CASCADE)
@NotNull
@ManyToOne(fetch = FetchType.LAZY,optional = false)
@JoinColumn(name = "REQUEST_STATE_ID")
protected RequestState requestState;
//other non related attribute and getter and setters go here
}
@Table(name = "VP_REQUEST")
@Entity(name = "vp$Request")
public class Request extends BaseUuidEntity {
private static final long serialVersionUID = 7073994286444570051L;
@Column(name = "REQUEST_NUMBER")
protected String requestNumber;
@Column(name = "REQUEST_KIND")
protected Integer requestKind;
@Column(name = "SUBJECT_TYPE")
protected Integer subjectType;
@Column(name = "SUBJECT_ID")
protected UUID subjectId;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "SUBMISSION_DATE")
protected Date submissionDate;
@Composition
@OnetoMany(mappedBy = "request")
protected List states;
//other non related attributes and setters and getters go here.
}
解决方法
这是我做的另一个试验,但我得到了一个不同的例外
public List leaveRequestsReport(List employeesGroupIds) {
if(employeesGroupIds!=null && employeesGroupIds.size()==0)
employeesGroupIds =null;
return persistence.getEntityManager().
createQuery("select new com.company.vp.workflow.LeaveRequestRow(rq.requestNumber,"
+ "e.employeeNumber,e.fullName,rq.submissionDate,eg.name,rq.states) "
+ " from vp$RequestStateAttribute at "
+ " INNER JOIN at.requestState st INNER JOIN at.requestState.request rq,"
+ " vp$Employee e INNER JOIN e.employeesGroup eg"
+ " where rq.subjectType = 10 "
+ " and rq.requestKind = 20 " )
.getResultList();
}
我得到的异常是
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:Exception Description:
Problem compiling [select new com.company.vp.workflow.LeaveRequestRow(rq.requestNumber,e.employeeNumber,rq.states) from vp$RequestStateAttribute at INNER JOIN at.requestState st INNER JOIN at.requestState.request rq,vp$Employee e INNER JOIN e.employeesGroup eg where rq.subjectType = 10 and rq.requestKind = 20 ].[127,136] The state field path 'rq.states' cannot be resolved to a collection type.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1750) ~[org.eclipse.persistence.jpa-2.7.3-7-cuba.jar:na]
at com.haulmont.cuba.core.sys.QueryImpl.buildJPAQuery(QueryImpl.java:229) ~[cuba-core-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.sys.QueryImpl.getQuery(QueryImpl.java:138) ~[cuba-core-7.2.6.jar:7.2.6]
at com.haulmont.cuba.core.sys.QueryImpl.getResultList(QueryImpl.java:409) ~[cuba-core-7.2.6.jar:7.2.6]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。