0

使用 EclipseLink-JPA2 我正在尝试创建一个动态查询。下面的示例代码:

TypedQuery query = getEntityManager().createQuery("UPDATE Employee e SET e.employeeStatus.code = :statusCode WHERE e.employeeId = :employeeId", Employee.class);

下面的EmployeeBean:

@Entity
@Table(name = "employee", uniqueConstraints = {
@UniqueConstraint(columnNames = {"employee_id"})})
@XmlRootElement
public class Employee implements Serializable {
@Id 
@Basic(optional = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id", nullable = false)
private Integer employeeId;    
@JoinColumn(name = "employee_status", referencedColumnName = "code", nullable = false)
@ManyToOne(optional = false)
private EmployeeStatus employeeStatus;

//Other fields here

public EmployeeStatus getEmployeeStatus() {
    return employeeStatus;
}
public void setEmployeeStatus(EmployeeStatus employeeStatus) {
    this.employeeStatus = employeeStatus;
}
//Other getters-setters here

}

下面的 EmployeeStatus Bean:

@Entity
@Table(name = "employee_status")
@XmlRootElement

public class EmployeeStatus implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "code", nullable = false)
private Integer code;
@Column(name = "descr", length = 32)
private String descr;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "employeeStatus")
private List<Employee> employeeList;

//Other fields here

@XmlTransient
public List<Employee> getEmployeeList() {
    return employeeList;
}
public void setEmployeeList(List<Employee> employeeList) {
    this.employeeList = employeeList;
}
//Other getters-setters here

}

在运行时,我在执行上述行期间收到以下错误:

编译查询时出错 无效导航表达式 [e.employeeStatus],无法导航 SET 子句目标中的关联字段 [employeeStatus]。

为什么会这样?如果我尝试更新一个简单的列(字符串,int ..),则对象类型不会正常执行。我必须在我的实体 bean 中设置任何属性吗?

4

0 回答 0