JPQL:未知状态或关联字段(EclipseLink)



我有一个从个人和组织单位继承的员工实体:

组织单位:

@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;
    }
    // ...
}

人:

@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;
    }
    // ...
}

员工实体:

@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...
}

然后,我尝试使用上面的查询通过正式姓名查找员工,例如"Doe,John":

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

但是,这给了我部署到 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].

问:

怎么了?是否禁止在 JPQL 中使用"人工"属性,这里是 WHERE 子句?这里的前提是什么?

我检查了很多次大写和拼写,我不走运。

您的类没有 formalName 的映射,因此无法在查询中使用。 您只能访问查询中的映射。 无论如何,数据库中没有 FormalName,所以我看不出如何使用它。

需要更改查询以访问名字和姓氏,就像您在 getFormalName 代码中尝试执行的操作一样:

"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"

请注意,您的 OrganizationalUnit 类在其字段上具有注释,因此只有字段才会具有默认映射。 这意味着您的表中将包含 firstName、LastName AND Name 字段,尽管 getName in Person 将 name 定义为 this.firstName + " " + this.lastName。 我建议在 Person 中删除 firstName/lastName 字段,而是处理名称字段,或者您可以从 OrganizationalUnit 中删除名称字段,让子类类以自己的方式处理 get/setname 访问器。

最新更新