0

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.

4

1 回答 1

2

您的类没有正式名称的映射,因此不能在查询中使用。您只能访问查询中的映射。无论如何,数据库中没有 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 和 Name 字段,尽管 Person 中的 getName 将名称定义为 this.firstName + " " + this.lastName。我建议删除 Person 中的 firstName/lastName 字段,而是处理 name 字段,或者您可以从 OrganizationalUnit 中删除 name 字段,并让子类以自己的方式处理 get/setname 访问器。

于 2012-11-22T16:57:43.793 回答