我有几个 JPA 实体,每个实体都有一个数据库用户列,在该列中我必须存储对表中特定行进行更改的用户。
我创建了一个'MappedSuperclass' Bean,所有实体都将从中扩展,这就是 MappedSuperclass。
@MappedSuperclass
public abstract class AuditableBean {
@Column(name = "modifier_user", nullable = false)
private String modifier_user;
// Getters and Setters
}
这是一个从“MappedSuperclass”扩展而来的实体。
@Entity
@Table(name = "nm_area_ref")
public class ServingAreaReferenceBean extends AuditableBean {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "nm_area_ref_id")
private UUID id;
@Column(name = "nm_srv_area_desc", nullable = false)
private String description;
@Column(name = "nm_retired", nullable = false)
private boolean retired;
// Getters and Setters
}
并且,所有的 Bean 都有一个对应的服务方法,用于将数据保存在数据库中,这是服务类之一(每个服务注入一个用于 CRUD 操作的存储库)。
// Service
@Component
public class ServingAreaReferenceBO {
@Inject private ServingAreaReferenceRepository repository; //repository injection
@Inject private CloudContextProvider cloudContextProvider;
public List<ServingAreaReferenceBean> getAllServingAreaReferences() {
return Lists.newArrayList(repository.findAll());
}
public Optional<ServingAreaReferenceBean> findById(UUID id) {
return repository.findById(id);
}
public ServingAreaReferenceBean create(ServingAreaReferenceBean servingAreaReference) {
Optional<CloudContext> cloudContext = Optional.ofNullable(cloudContextProvider.get());// line 1
servingAreaReference.setUpdaterUser(cloudContext.map(CloudContext::getUserId).orElse(null));// line 2
return repository.save(servingAreaReference);// line 3
}
}
// Repository - It extends from CrudRepository (insert, update, delete operations)
@Repository
public interface ServingAreaReferenceRepository extends CrudRepository<ServingAreaReferenceBean, UUID> {
boolean existsByDescription(String description);
boolean existsByDescriptionAndIdIsNot(String description, UUID id);
}
当“repository.save()”(第 3 行)执行时,它成功存储了用户,但我将用户逻辑放在执行保存方法之前(第 1、2 行)。所以我不认为在每个服务上重复这两行是最好的方法,相反,我想实现一个通用方法或一个通用类,在执行保存方法之前为所有 Bean 实体设置用户。
那可能吗?什么是更好的方法?
我想实现这样的东西,但不知道如何使它通用?
@Component
public class AuditableBeanHandler {
@Inject private CloudContextProvider cloudContextProvider;
public AuditableBean populateAuditInformation(AuditableBean auditableBean) {
Optional<CloudContext> cloudContext = Optional.ofNullable(CloudContextProvider.get());
auditableBean.setUpdaterUser(CloudContext.map(cloudContext::getUserId).orElse(null));
return auditableBean;
}
}