我有以下 JPA (2.0.2) 实体:
员工
@Entity
@Table(name = "T_EMPLOYEE")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ElementCollection
@CollectionTable(name = "T_COMPETENCE")
private Set<Competence> competences;
// Getter and setters
}
和能力
@Embeddable
public class Competence {
@JoinColumn(nullable = false)
@ManyToOne
private Skill skill;
// Getter and setters
}
(技能实体不应该很重要,因此被省略,各种附加属性也是如此。)
我正在使用 EclipseLink (2.2.0) 通过 DAO 查询我的实体。现在我想使用以下查询:
public List<Employee> findBySkill(Skill skill) {
TypedQuery<Employee> query = getCurrentEntityManager().createQuery(
"SELECT e FROM Employee e JOIN e.competences c WHERE c.skill = :skill",
Employee.class);
query.setParameter("skill", skill);
return query.getResultList();
}
但它不断抛出以下异常:
Caused by: Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [SELECT e FROM Employee e JOIN e.competences c WHERE c.skill = :skill], line 1, column 54: unknown state or association field [skill] of class [com.kaio.model.Competence].
at org.eclipse.persistence.exceptions.JPQLException.unknownAttribute(JPQLException.java:457)
at org.eclipse.persistence.internal.jpa.parsing.DotNode.validate(DotNode.java:88)
at org.eclipse.persistence.internal.jpa.parsing.Node.validate(Node.java:91)
at org.eclipse.persistence.internal.jpa.parsing.BinaryOperatorNode.validate(BinaryOperatorNode.java:34)
at org.eclipse.persistence.internal.jpa.parsing.EqualsNode.validate(EqualsNode.java:41)
at org.eclipse.persistence.internal.jpa.parsing.WhereNode.validate(WhereNode.java:34)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:207)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:183)
at org.eclipse.persistence.internal.jpa.parsing.ParseTree.validate(ParseTree.java:173)
at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateReadQueryInternal(JPQLParseTree.java:110)
at org.eclipse.persistence.internal.jpa.parsing.JPQLParseTree.populateQuery(JPQLParseTree.java:84)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:216)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:187)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:139)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:123)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1376)
... 48 more
消息很清楚:在我的职业能力上找不到属性技能。但在我看来,没有理由这样做。还是我对我的问题有错误的方法?我应该如何在可嵌入对象列表中查询?
任何帮助表示赞赏。