简体   繁体   中英

JPQL: unknown state or association field (EclipseLink)

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, eg "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.

Your class doesn't have a mapping for formalName, so it cannot be used in a query. You can only accesss mappings within your queries. There is no FormalName in the database anyway, so I don't see how it could be used.

Your query will need to be changed to access the first and last name the same way you attempt to do in the getFormalName code:

"SELECT emp FROM Employee emp JOIN FETCH emp.client JOIN FETCH emp.unit WHERE emp.client.id = :clientId AND CONCAT(CONCAT(emp.lastName, ', '), emp.firstName) = :formalName"

Note that your OrganizationalUnit class has annotations on its fields, so only fields will have default mappings. This means your table will have firstName, LastName AND Name fields in it, despite getName in Person defining name as this.firstName + " " + this.lastName. I'd recommend removing the firstName/lastName fields in Person and instead dealing with the name field, or you can remove the name field from OrganizationalUnit and let subclasses classes handle the get/setname accessors in their own way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM