EJB クラスには、リモート インターフェイスを持つ 2 つのメソッドがあります。
Class MyBean {
public CustomerEntity getCustomer(String id) {......}
public void updateCustomer(CustomerEntity newValues, CustomerEntity oldValues) {......}
}
顧客エンティティは、ゲッターとセッターを持ついくつかのフィールドで構成されています。
@Entity
public class Customer {
@ID private String id;
@Column private String name;
@Column private String phone;
// Getters and setters
.
.
}
クライアント アプリは次のことを行います。
Customer customer myBeanRemoteInterface.getCustomer("some id");
Customer oldCustomer = customer; //Save original customer data
displayCustomerFormAndAcceptChanges(customer);
myBeanRemoteInterface.updateCustomer(customer, oldCustomer);
EJB updateCustomer は、サーバー上の顧客を更新する必要があります。他のユーザーが他のフィールドに加えた変更を上書きしないようにするには、ユーザーが変更したフィールドのみをコミットする必要があります。次のように:
public void updateCustomer(CustomerEntity newValues, CustomerEntity oldValues) {
Customer customer = entityManager.find(Customer.class, oldValues.getId());
if (!newValues.getName().equals(oldValues.getName()) { // Value updated
// If the value fetched by entityManager.find is different from what was originally fetched that indicates that the value has been updated by another user.
if (!customer.getName().equals(oldValues.getName()) throw new CustomerUpdatedByOtherUserException();
else customer.setName(newValues.getName());
}
// repeat the code block for every field in Customer class
entityManager.flush();
}
ここでの問題は、updateCustomer のコード ブロックを Customer クラスのフィールドごとに 1 回繰り返す必要があることです。Customer クラスに新しいフィールドが挿入された場合は、EJB も更新する必要があります。
Customer クラスにさらにフィールドが追加された場合に、EJB を更新する必要なく機能するソリューションが必要です。
助言がありますか?