因此,为了让生活更轻松,我想出一个注释来标记我有兴趣比较的字段。没有我最终不得不坚持命名对话,就像只使用以“get”开头的方法一样。我发现这种方法有很多极端情况。
注释。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AuditCompare {
public String name() default "";
public CompareType compareBy() default CompareType.string;
enum CompareType {
string, count
}
}
使用起来像
@Entity
@Audited
public class Guideline {
.....
@AuditCompare
private String name;
@AuditCompare
private String owner;
@OneToMany(cascade = CascadeType.ALL, orphanRemoval=true, mappedBy="guideline")
private Set<GuidelineCheckListItem> checkListItems = new HashSet<GuidelineCheckListItem>();
.........
}
由于 envers 将 Set 更改和 set 的对象作为两个不同的事件进行审核,因此我不想在 set 更改时进行比较。然后进行比较,我的方法看起来像
private void findMatchingValues(Object oldInstance, Object newInstance, ActivityEntry entry) {
try {
Class oldClass = oldInstance.getClass();
for (Field someField : oldClass.getDeclaredFields()) {
if (someField.isAnnotationPresent(AuditCompare.class)) {
String name = someField.getAnnotation(AuditCompare.class).name();
name = name.equals("") ? someField.getName() : name;
Method method = oldClass.getDeclaredMethod(getGetterName(name));
if(someField.getAnnotation(AuditCompare.class).compareBy().equals(AuditCompare.CompareType.count)) {
int oldSize = getCollectionCount(oldInstance, method);
int newSize = getCollectionCount(newInstance, method);
if (oldSize != newSize) entry.addChangeEntry(name, oldSize, newSize);
} else {
Object oldValue = getObjectValue(oldInstance, method);
Object newValue = getObjectValue(newInstance, method);
if (!oldValue.equals(newValue)) entry.addChangeEntry(name, oldValue, newValue);
}
}
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}