gpt4 book ai didi

java - JPQL : unknown state or association field (EclipseLink)

转载 作者:行者123 更新时间:2023-12-02 07:26:30 25 4
gpt4 key购买 nike

我有一个继承自 Person 和 OrganizationalUnit 的 Employee 实体:

组织单位:

@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):

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 类在其字段上有注释,因此只有字段才会具有默认映射。这意味着您的表中将包含名字、姓氏和名称字段,尽管 Person 中的 getName 将名称定义为 this.firstName + ""+ this.lastName。我建议删除 Person 中的firstName/lastName 字段并改为处理名称字段,或者您可以从 OrganizationalUnit 中删除名称字段并让子类以自己的方式处理 get/setname 访问器。

关于java - JPQL : unknown state or association field (EclipseLink),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13515279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com