【发布时间】:2023-04-03 21:20:02
【问题描述】:
所以我有以下情况:
我有一个包含大量字段(超过 20 个)的客户实体。客户 类看起来像这样(我只列出了前几个字段):
@Entity
public class Customer {
@Id
@GeneratedValue
private long id;
@Version
private version version;
private String firstName;
private String lastName;
private String email;
.
.
.
}
现在我有一个方法可以通过 RESTful Web 服务接收客户的所有值来更新客户。服务方法看起来像这样:
@PUT
@Path("{id}")
@Consumes("application/json")
@Produces("application/json")
public Response editCustomer(@PathParam("id") long id, Customer customer) {
return FacadeUtils.buildResponse(updateCustomer(customer, id));
}
我的 updateCustomer 看起来像这样:
public Result updateCustomer(Customer customer, long id) {
@PersistenceContext
private EntityManager em;
customer.setId(id);
em.merge(customer);
.
.
.
}
但是,当我尝试执行更新时,出现以下异常:
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
我知道我可以使用oldCustomer = em.find(Customer.class, id); 将对象加载到持久化上下文中,然后使用oldCustomer.setXXX(customer.getXXX); 设置我需要的所有值
但是,由于我有很多字段要更新(有时所有 20 个字段都会更改,所以这样做感觉不对。
我知道我可以尝试使用反射或 Apache Bean Utils 来复制字段,但必须有一些更优雅的解决方案。有什么想法吗?
【问题讨论】: