JPA:PersistenceException“未知状态或关联字段”

时间:2011-11-10 22:14:51

标签: java-ee jpa eclipselink jpql

我试图了解如何在JPQL中进行(简单)连接。我有两个表/实体:父和子。

@Entity
@Table(name = "Parent")
@NamedQueries({
    @NamedQuery(name = "Parent.findByStatus", query = "SELECT p FROM Parent p WHERE p.status = :status")})
public class Parent
{
    private String parentId;
    private Set<Child> children;
    private String status;

    public Parent()
    {
        // empty constructor
    }

    @Id
    @Column(name = "PARENT_ID")
    public String getParentId()
    {
        return parentId;
    }

    public void setParentId(String parentId)
    {
        this.parentId= parentId;
    }

    // bi-directional many-to-one association to Child
    @OneToMany(mappedBy = "parent")
    public Set<Child> getChildren()
    {
        return children;
    }

    public void setChildre(Set<Child> children)
    {
        this.children= children;
    }
    @Column(name = "STATUS")
    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }
}

@Entity
@NamedQueries({
        @NamedQuery(name = "Child.findByStatus", query = "SELECT c FROM Child c WHERE c.status = :status"),
        @NamedQuery(name = "Child.findByParentId", query = "SELECT c FROM Child c JOIN c.parent p WHERE p.parentId = :parentId"),
        @NamedQuery(name = "Child.findByParentIdAndStatus", query = "SELECT c FROM Child c JOIN c.parent p WHERE p.parentId = :parentId AND c.status = :status") })
public class Child
{
    private childId id;
    private String status;
    private Parent parent;

    public Child()
    {
        // empty constructor
    }

    @Id
    @Column(name = "CHILD_ID")
    public String getChildId()
    {
        return childId;
    }

    public void setChildId(String childId)
    {
        this.childId= childId;
    }

    // bi-directional many-to-one association to Parent 
    @ManyToOne
    @JoinColumn(name = "PARENT_ID")
    public Parent getParent ()
    {
        return parent;
    }

    public void setParent(Parent parent)
    {
        this.parent= parent;
    }

    @Column(name = "STATUS")
    public String getStatus()
    {
        return status;
    }

    public void setStatus(String status)
    {
        this.status = status;
    }
}

但是这会导致以下异常:

Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [Child.findByParentId: SELECT c FROM Child c JOIN c.parent p WHERE p.parentId = :parentId], line 1, column 47: unknown state or association field [parentId] of class [my.model.Parent].

我的理解是JPQL在JOIN上遍历对象模型。所以,在查询中:

SELECT c FROM Child c JOIN c.parent p WHERE p.parentId = :parentId

p是与Child对象关联的获取的Parent对象。如果这是正确的,那么为什么我收到错误? Parent对象有一个parentId字段,所以应该没问题,是吗?

2 个答案:

答案 0 :(得分:2)

您的JPQL看起来有效。检查您是否正确编译了代码,(您最近是否重命名了该字段?)。

查询任何其他字段是否有效?如果您将字段重命名为id,该怎么办?

答案 1 :(得分:0)

您可以使用Child的字段来实现此目的,

from Child c where c.parent.id = :parent_id

或者只是你有一个父母的参考,而不仅仅是id。

from Child c where c.parent = :parent

hibernate将找出连接本身。