在 EJB 类中,我有两种带有远程接口的方法:
Class MyBean {
public CustomerEntity getCustomer(String id) {......}
public void updateCustomer(CustomerEntity newValues, CustomerEntity oldValues) {......}
}
客户实体由一些带有 getter 和 setter 的字段组成。
@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 类中的每个字段重复一次。如果将新字段插入到 Customer 类中,则 EJB 也需要更新。
如果将更多字段添加到 Customer 类,我需要一个无需更新 EJB 即可工作的解决方案。
有什么建议么?