Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » PersistenceException "unknown state or association field"
PersistenceException "unknown state or association field" [message #756107] Thu, 10 November 2011 23:16 Go to next message
Shelli Orton is currently offline Shelli OrtonFriend
Messages: 76
Registered: September 2009
Member
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?
Re: PersistenceException "unknown state or association field" [message #756230 is a reply to message #756107] Fri, 11 November 2011 15:18 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Yes, your query looks correct. Check that if you add a simple "Select p from Parent P where p.parentId = :parentId" you get the same issue, and turn EclipseLink logging to fine or Finest:
<property name="eclipselink.logging.level" value="FINE"/>
and see if there are any warnings that indicate a problem with how the Parent class is processed.

It is probably a cut and paste error, but i noticed a spelling mistake in the setChildre method on the parent that might affect this mapping being processed.

Best Regards,
Chris
Re: PersistenceException &quot;unknown state or association field&quot; [message #756232 is a reply to message #756107] Fri, 11 November 2011 15:18 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1039
Registered: July 2009
Senior Member
Yes, your query looks correct. Check that if you add a simple "Select p from Parent P where p.parentId = :parentId" you get the same issue, and turn EclipseLink logging to fine or Finest:
<property name="eclipselink.logging.level" value="FINE"/>
and see if there are any warnings that indicate a problem with how the Parent class is processed.

It is probably a cut and paste error, but i noticed a spelling mistake in the setChildre method on the parent that might affect this mapping being processed.

Best Regards,
Chris
Re: PersistenceException &quot;unknown state or association field&quot; [message #756716 is a reply to message #756232] Mon, 14 November 2011 19:11 Go to previous messageGo to next message
Shelli Orton is currently offline Shelli OrtonFriend
Messages: 76
Registered: September 2009
Member
Yes, that's a copy/paste error.

If I comment out the queries in Child that have joins to Parent, I can successfully execute the "Parent.findByStatus" query. So, there must be something wrong with my joins on Child.

I have set the logging level to "FINE" in my persistence.xml. However, I'm not getting any logging other than my own log statements in the service bean (and the exception). Perhaps this needs to be set in Glassfish itself?
Re: PersistenceException "unknown state or association field" [message #756958 is a reply to message #756230] Tue, 15 November 2011 17:46 Go to previous message
Shelli Orton is currently offline Shelli OrtonFriend
Messages: 76
Registered: September 2009
Member
The root of the issue was in the naming of the variables versus the methods with slight differences in capitalization between the two (e.g. "parentID" versus "getParentId" - the 'd' being different). In the query I was using the variable capitalization and EclipseLink was looking for the method name version.
Previous Topic:IllegalArgumentException: eclipselink.profiler
Next Topic:Descriptor Events on Entities which have not been modified during a Transaction
Goto Forum:
  


Current Time: Sun Nov 23 08:51:42 GMT 2014

Powered by FUDForum. Page generated in 0.01768 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software