JPQL: unknown state or association field (EclipseLink)

Posted by Kawu on Stack Overflow See other posts from Stack Overflow or by Kawu
Published on 2012-11-22T14:55:01Z Indexed on 2012/11/22 16:59 UTC
Read the original article Hit count: 778

Filed under:
|
|
|
|

I have an Employee entity which inherits from Person and OrganizationalUnit:

OrganizationalUnit:

@MappedSuperclass
public abstract class OrganizationalUnit implements Serializable
{
    @Id
    private Long id;

    @Basic( optional = false )
    private String name;

    public Long getId()
    {
        return this.id;
    }

    public void setId( Long id )
    {
        this.id = id;
    }

    public String getName()
    {
        return this.name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    // ...
}

Person:

@MappedSuperclass
public abstract class Person extends OrganizationalUnit
{
    private String lastName;

    private String firstName;

    public String getLastName()
    {
        return this.lastName;
    }

    public void setLastName( String lastName )
    {
        this.lastName = lastName;
    }

    public String getFirstName()
    {
        return this.firstName;
    }

    public void setFirstName( String firstName )
    {
        this.firstName = firstName;
    }

    /**
     * Returns names of the form "John Doe".
     */
    @Override
    public String getName()
    {
        return this.firstName + " " + this.lastName;
    }

    @Override
    public void setName( String name )
    {
        throw new UnsupportedOperationException( "Name cannot be set explicitly!" );
    }

    /**
     * Returns names of the form "Doe, John".
     */
    public String getFormalName()
    {
        return this.lastName + ", " + this.firstName;
    }

    // ...
}

Employee entity:

@Entity
@Table( name = "EMPLOYEES" )
@AttributeOverrides
(
    {
        @AttributeOverride( name = "id", column = @Column( name = "EMPLOYEE_ID" ) ),
        @AttributeOverride( name = "name", column = @Column( name = "LASTNAME", insertable = false, updatable = false ) ),
        @AttributeOverride( name = "firstName", column = @Column( name = "FIRSTNAME" ) ),
        @AttributeOverride( name = "lastName", column = @Column( name = "LASTNAME" ) ),
    }
)
@NamedQueries
(
    {
        @NamedQuery( name  = "Employee.FIND_BY_FORMAL_NAME",
                     query = "SELECT emp " +
                             "FROM Employee emp " +
                             "WHERE emp.formalName = :formalName" )
    }
)
public class Employee extends Person
{
    @Column( name = "EMPLOYEE_NO" )
    private String nbr;

    // lots of other stuff...
}

I then attempted to find an employee by its formal name, e.g. "Doe, John" using the query above:

SELECT emp
FROM Employee emp
WHERE emp.formalName = :formalName

However, this gives me an exception on deploying to EclipseLink:

Exception while preparing the app : Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [Employee.FIND_BY_CLIENT_AND_FORMAL_NAME: SELECT emp FROM Employee emp   JOIN FETCH emp.client   JOIN FETCH emp.unit WHERE emp.client.id = :clientId AND emp.formalName = :formalName], line 1, column 115: unknown state or association field [formalName] of class [de.bnext.core.common.entity.Employee].
Local Exception Stack: 
Exception [EclipseLink-8030] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.JPQLException
Exception Description: Error compiling the query [Employee.FIND_BY_CLIENT_AND_FORMAL_NAME: SELECT emp FROM Employee emp   JOIN FETCH emp.client   JOIN FETCH emp.unit WHERE emp.client.id = :clientId AND emp.formalName = :formalName], line 1, column 115: unknown state or association field [formalName] of class [de.bnext.core.common.entity.Employee].

Qs:

What's wrong? Is it prohibited to use "artificial" properties in JPQL, here the WHERE clause? What are the premises here?

I checked the capitalization and spelling many times, I'm out of luck.

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate