如何使用 Spring JPA 在 JPQL 中用另一个对象值更新一个对象
How to update an object with another object value in JPQL using Spring JPA
我在 JPQL 中遇到问题。我有两个如下所示的实体
class Employee{
private Long id;
private String name;
private Department department;
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setDepartment(Department department){
this.department = department
}
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public Department getDepartment(){
return this.department;
}
}
和...
class Department{
private Long id;
private String name;
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public Long getId(){
return id;
}
public String getName(){
return name;
}
}
现在我需要更新 Employee
的部门。我试过下面的查询。
update Employee e set e.department.id = 'XXX' where e.id in (?1);
这是异常
java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations.
你能指导我吗,我该如何解决这个问题?
干杯,
泰迦.
在您的 Spring 数据 JPA 存储库界面中执行:
interface EmployeeRepository extends Repository<Employee, Long> {
@Modifying
@Transactional
@Query("update Employee e set e.department = ?2 where e = ?1")
void updateDepartment(Employee employee, Department department);
}
请务必了解:
- 如果您正在执行修改查询,您将绕过实体上的生命周期回调。这是 JPA 的一个基本特征。
- 如果您需要应用生命周期回调,加载
Employee
,手动设置 Department
,存储 Employee
。
@Modifying(clearAutomatically = true)
@Transactional
@Query("update Employee e set e.department = ?2 where e = ?1")
void updateDepartment(Employee employee, Department department);
@Modifying 会将其与 select 查询分开。
@Transactional 将有助于与数据库进行交易。
@Query 是相同的旧查询执行。
我在 JPQL 中遇到问题。我有两个如下所示的实体
class Employee{
private Long id;
private String name;
private Department department;
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setDepartment(Department department){
this.department = department
}
public Long getId(){
return this.id;
}
public String getName(){
return this.name;
}
public Department getDepartment(){
return this.department;
}
}
和...
class Department{
private Long id;
private String name;
public void setId(Long id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public Long getId(){
return id;
}
public String getName(){
return name;
}
}
现在我需要更新 Employee
的部门。我试过下面的查询。
update Employee e set e.department.id = 'XXX' where e.id in (?1);
这是异常
java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations.
你能指导我吗,我该如何解决这个问题?
干杯, 泰迦.
在您的 Spring 数据 JPA 存储库界面中执行:
interface EmployeeRepository extends Repository<Employee, Long> {
@Modifying
@Transactional
@Query("update Employee e set e.department = ?2 where e = ?1")
void updateDepartment(Employee employee, Department department);
}
请务必了解:
- 如果您正在执行修改查询,您将绕过实体上的生命周期回调。这是 JPA 的一个基本特征。
- 如果您需要应用生命周期回调,加载
Employee
,手动设置Department
,存储Employee
。
@Modifying(clearAutomatically = true)
@Transactional
@Query("update Employee e set e.department = ?2 where e = ?1")
void updateDepartment(Employee employee, Department department);
@Modifying 会将其与 select 查询分开。
@Transactional 将有助于与数据库进行交易。
@Query 是相同的旧查询执行。