我有两个表(JPA实体)
JOB
申请者
JOBID是这两个表之间的公共列。 我正在尝试选择用户已应用的作业实体(申请人表具有userId字段)
SELECT j from JOB j, APPLICANTS a WHERE j.jobId=:a.jobId AND a.userId=:userId
query.setParameter("userId", userId);
这会引发以下错误
java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing [SELECT j from JOB j, APPLICANTS a WHERE j.jobId=:a.jobId AND a.uuId=:uuId].
[50, 58] The named input parameter ''{0}'' is not following the rules for a Java identifier.
org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
答案 0 :(得分:4)
您不必在WHERE
中使用:
为字段值添加前缀,它仅用于参数。因此,如果您想加入Job
上的Applicant
和jobId
,请使用此
SELECT j from JOB j, APPLICANTS a WHERE j.jobId = a.jobId AND a.userId = :userId
query.setParameter("userId", userId);
如果您想将jobId
作为参数传递,请使用此
SELECT j from JOB j, APPLICANTS a WHERE j.jobId = :jobId AND a.userId = :userId
query.setParameter("userId", userId);
query.setParameter("jobId", jobId);
答案 1 :(得分:2)
您需要使用Annotation指定Job和Applicants实体之间的关系 申请人实体
@manyToOne(fetch=FetchType.LAZY)
@joinColumns(name="job_id", insertable=false , updatable=false)
private Job job ;
你的查询应该是这样的
select NEW YourClass( Your Column List ) from Applicants a left join a.job where j.jobId=?1 and a.userId=?2
query.setParameter(1, userId);
query.setParameter(2, jobId);