Querying a collection of embeddable elements using eclipselink

大憨熊 提交于 2019-12-08 07:53:02

问题


I have the following JPA (2.0.2) entities:

Employee

@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
}

and Competence

@Embeddable
public class Competence {
    @JoinColumn(nullable = false)
    @ManyToOne
    private Skill skill;

    // Getter and setters
}

(The skill entity shouldn't be important and is therefore omitted, so are various additional attributes.)

I'm using EclipseLink (2.2.0) to query my entities with a DAO. Now I'd like to use the following query:

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();
}

But it keeps throwing the following exception:

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

The message is pretty clear: It can't find the attribute Skill on my class Competence. But in my opinion there is no reason on that. Or do I have a wrong approach on my problem? How should I query in a List of embeddable objects?

Any help is appreciated.


回答1:


Okay, this seems to be a bug and it has been solved in a later version of eclipse link. I updated the project to EclipseLink 2.4 and the problem disappeared.




回答2:


Your Competence table will have a Skill.id field, so try:

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

query.setParameter("skillId", skill.getId());


来源:https://stackoverflow.com/questions/11768528/querying-a-collection-of-embeddable-elements-using-eclipselink

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!