JPA 2 - 无法导航关联字段

时间:2013-08-07 14:00:36

标签: jpa

使用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]。

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

0 个答案:

没有答案