我有一个继承自Person和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;
}
// ...
}
人:
@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].
Qs的:
怎么了?是否禁止在JPQL中使用“人工”属性,这里是WHERE子句?这里的前提是什么?
我多次查看大写和拼写,我运气不好。
答案 0 :(得分:2)
您的类没有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和Name字段,尽管getName in Person定义名称为this.firstName +“”+ this.lastName。我建议删除Person中的firstName / lastName字段,而不是处理name字段,或者你可以从OrganizationalUnit中删除name字段,让子类以自己的方式处理get / setname访问器。