I am trying to understand how to do (simple) joins in JPQL. I have two tables/entities: Parent and Child.
@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;
}
}
But this causes the following exception:
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].
It's my understanding that JPQL traverses the object model on JOINs. So, in the query:
SELECT c FROM Child c JOIN c.parent p WHERE p.parentId = :parentId
p is the fetched Parent object associated with the Child objects. If this is correct, then why I am getting the error? The Parent object has a parentId field and so should be okay, yes?
Your JPQL looks valid. Check that you compiled your code correctly, (did you recently rename the field?).
Does querying any of the other fields work? What if you rename the field to id?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With