1

我有以下 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

消息很清楚:在我的职业能力上找不到属性技能。但在我看来,没有理由这样做。还是我对我的问题有错误的方法?我应该如何在可嵌入对象列表中查询?

任何帮助表示赞赏。

4

2 回答 2

1

好的,这似乎是一个错误,并且已在更高版本的eclipse链接中解决。我将项目更新到 EclipseLink 2.4,问题就消失了。

于 2012-08-02T12:56:14.343 回答
0

您的Competence表将有一个 Skill.id 字段,因此请尝试:

"SELECT e FROM Employee e JOIN e.competences c WHERE c.skill.id = :skillId"

query.setParameter("skillId", skill.getId());
于 2012-08-02T05:05:57.017 回答