0

Using EclipseLink-JPA2 I'm trying to create a dynamic query. Sample code below:

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

EmployeeBean below:

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

@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

}

During runtime I get the following error during the execution of the above line:

Error compiling the query invalid navigation expression [e.employeeStatus], cannot navigate association field [employeeStatus] in the SET clause target.

Why is this happening? If I try to update a simple column(String,int..) not object type is executed normally. Do I have to set any property inside my entity bean?

user2644660
  • 1
  • 1
  • 2
  • 2
    1) Your query is missing a "`=`" in the `WHERE` clause. Let's assume that's a typo, and focus on 2) We really need to see how your `Employee` is defined. Yes, you need a `setEmployeeStatus(..)` method defined in `Employee` - unless you have [specified field level access for JPA](http://stackoverflow.com/questions/13874528/what-is-the-purpose-of-accesstype-field-accesstype-property-and-access). – Richard Sitze Aug 07 '13 at 14:07
  • you are missing = between `e.employeeId :employeeId`. provide mapping – Luca Basso Ricci Aug 07 '13 at 14:08
  • You need to use something like JOIN for your query. But you can't do this in UPDATE statement. So please try to "rephrase" your query for EmployeeStatus entity, if such exists. – n1ckolas Aug 07 '13 at 14:10
  • I have post the entity beans....Instead of creating the update query(and have this problem) is it better to use merge(entity) function? – user2644660 Aug 08 '13 at 08:35
  • @user2644660 yes, that would be more ORM-way – n1ckolas Aug 09 '13 at 08:10

0 Answers0