gpt4 book ai didi

jakarta-ee - JPA:PersistenceException "unknown state or association field"

转载 作者:行者123 更新时间:2023-12-03 13:21:31 24 4
gpt4 key购买 nike

我想了解如何在 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 是获取的与子对象关联的父对象。如果这是正确的,那么为什么我会收到错误消息? Parent 对象有一个 parentId 字段,所以应该没问题,是吗?

最佳答案

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

查询任何其他字段是否有效?如果将字段重命名为 id 会怎么样?

关于jakarta-ee - JPA:PersistenceException "unknown state or association field",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8087102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com