2

我在 JPA 中面临一个关于多对一关系的问题。我现在遇到的问题是使用 where 子句获取数据。

Skills班级:

    @Id
    private long skillsID;
    @Basic
    private String longDescription;
    @Basic
    private String shortDescription;
    @Basic
    private String colOrder;
    @Basic
    private String isActive;
    @Basic
    private String changeDate;
    @ManyToOne(fetch=FetchType.EAGER)
    private Category category;

第二个实体是:

Category 班级:

    @Id
    private long categoryID;
    @Basic
    private String name;
    @Basic
    private String colOrder;
    @Basic
    private String isActive;
    @Basic
    private String changedDate;
    @OneToMany(mappedBy = "category")
    private Collection<Skills> skills;

所以在 JPA 中,当我执行以下代码时,它会抛出异常,例如:

java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager: 

Exception Description: Problem compiling [SELECT s.longDescription  from Skills s, Category c  where s.categoryID =  c.categoryID]. 

[57, 69] The state field path 's.categoryID' cannot be resolved to a valid type.

JPA代码是:

Query q = em .createQuery("SELECT s.longDescription  from Skills s, Category c "+
"where s.categoryID =  c.categoryID");

那么,如何根据外键获取Skills数据Category

4

1 回答 1

1

实体categoryID中没有字段。Skills

只需在以下位置使用它的category字段Query

Query q = em .createQuery("SELECT s.longDescription from Skills s, Category c " +
                          "WHERE s.category =  c");
于 2013-06-30T00:27:28.993 回答