したがって、次のような状況があります。一時インスタンスを使用してJPAのエンティティをIDで更新します
私は20以上のフィールドを持つCustomerエンティティを持っています。
@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は次のようなものになります。
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
:私は更新を実行しようとすると、しかし
public Result updateCustomer(Customer customer, long id) {
@PersistenceContext
private EntityManager em;
customer.setId(id);
em.merge(customer);
.
.
.
}
を、私は次の例外を取得
oldCustomer = em.find(Customer.class, id);
を使用して永続コンテキストにオブジェクトをロードしてから、必要な値をすべて設定することができます。oldCustomer.setXXX(customer.getXXX);
しかし、私は多くのフィールドを更新する必要があります(20フィールドのすべてが変更されることもあります。
私はReflectionsやApache Bean Utilsを使ってフィールドのコピーを試みることができると知っていますが、もっと洗練されたソリューションが必要です。何か案は?