jpa复合主键表不返回值
Posted
技术标签:
【中文标题】jpa复合主键表不返回值【英文标题】:jpa composite primary key table not returning values 【发布时间】:2014-01-31 08:19:08 【问题描述】:我是新 JPA,当我从复合主键表中检索值时遇到异常。
异常说明:
Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t].
[14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
以下是我的代码
@Entity
@Table(name = "ASSIGN_TASK_EMPLOYEE")
//@IdClass(AssignTaskEmployeePk.class)
public class AssignTaskEmployee implements Serializable
@EmbeddedId
private AssignTaskEmployeePk assignTaskEmployeePk;
public AssignTaskEmployeePk getAssignTaskEmployeePk()
return assignTaskEmployeePk;
public void setAssignTaskEmployeePk(AssignTaskEmployeePk assignTaskEmployeePk)
this.assignTaskEmployeePk = assignTaskEmployeePk;
@Embeddable
public class AssignTaskEmployeePk
private String employeeId;
private String taskId;
public AssignTaskEmployeePk()
@Override
public boolean equals(Object obj)
// TODO Auto-generated method stub
if (obj instanceof AssignTaskEmployeePk)
AssignTaskEmployeePk employeePk = (AssignTaskEmployeePk) obj;
if (!employeePk.getEmployeeId().equals(this.employeeId))
return false;
else if (!employeePk.getTaskId().equals(this.taskId))
return false;
else
return false;
return false;
@Override
public int hashCode()
return employeeId.hashCode() + taskId.hashCode() ;
public String getEmployeeId()
return employeeId;
public void setEmployeeId(String employeeId)
this.employeeId = employeeId;
public String getTaskId()
return taskId;
public void setTaskId(String taskId)
this.taskId = taskId;
我在数据库中为组合主键 ASSIGN_TASK_EMPLOYEE(表)添加了四个值,其中 PK 表
EMP_ID TASKID
1 2
2 4
3 5
4 6
现在我想获得分配给 emp_id 1 的任务 为此,我编写了以下查询:这应该返回 AssignTaskEmployee 对象的列表。
entityManager.createQuery("select t from ASSIGN_TASK_EMPLOYEE t").getResultList()
当我执行这个查询时,我得到以下异常
Exception Description:
Problem compiling [select t from ASSIGN_TASK_EMPLOYEE t].
[14, 34] The abstract schema type 'ASSIGN_TASK_EMPLOYEE' is unknown.
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1605)
【问题讨论】:
【参考方案1】:JPQL 应该使用实体名称,默认是类的名称。分配任务员工
应该是
entityManager.createQuery("select t from AssignTaskEmployee t").getResultList()
以上将返回表 ASSIGN_TASK_EMPLOYEE 中的所有记录。
如果您想使用 JPQL 检索特定记录,您应该使用 WHERE 语句,如下所示:
Query query = entityManager.createQuery("select t from AssignTaskEmployee t WHERE
t.assignTaskEmployeePk.employeeId = :employeeId and t.assignTaskEmployeePk.taskId = :taskId")
query.setParameter("employeeId", 1);
query.setParameter("taskId",1);
query.getSingleResult() //As expected to have only one record.
阅读this查询EmbeddedId
【讨论】:
以上是关于jpa复合主键表不返回值的主要内容,如果未能解决你的问题,请参考以下文章