我正在尝试向JIRA显示UI上的更改历史记录。我正在使用Spring Data JPA
,并且已使用Envers(v5.3.7)配置了审计跟踪。我可以使用AuditQuery
通过其主键值获取特定实体的所有修订的列表。
是否有一种简便的方法来计算各个修订版之间的“增量”并确定已更改的属性? (具有新旧价值)
我已将@Audited(withModifiedFlag = true)
注释添加到我的实体类中。它在<entity>_aud
表中为每个属性添加了另外一列,指示该属性是否已更改。我试图弄清楚如何利用这些额外的列。
答案 0 :(得分:0)
如果您需要JIRA之类的东西,则必须自己构建。
我建议您使用休眠拦截器:
http://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#events
如以下示例所示,您将获得当前状态和先前状态,然后可以创建增量并将其存储在自己的变更日志表中:
For key in event["Sites"].__dict__:
data.append("{:.4f}".format(getattr(event["Sites"], key)[i]))
答案 1 :(得分:0)
这是我的代码
import javax.annotation.PostConstruct;
import javax.persistence.EntityManagerFactory;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PreUpdateEvent;
import org.hibernate.event.spi.PreUpdateEventListener;
import org.hibernate.internal.SessionFactoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class AuditListener implements PreUpdateEventListener {
@Autowired
private EntityManagerFactory entityManagerFactory;
@PostConstruct
private void init() {
SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
// You can also add listener for a specific entity-type instead of event-group
// In my case I needed global event listener
registry.getEventListenerGroup(EventType.PRE_UPDATE).appendListener(this);
}
@Override
public boolean onPreUpdate(PreUpdateEvent event) {
String[] propertyNames = event.getPersister().getPropertyNames();
Object[] oldValues = event.getOldState();
Object[] newValues = event.getState();
for (int index = 0; index < propertyNames.length; index++) {
String propertyName = propertyNames[index];
Object oldValue = oldValues[index];
Object newValue = newValues[index];
// This is just sample code
boolean changed = oldValue != newValue;
if (changed) {
System.out.println("Audit log -> Property: " + propertyName + ", Old value: " + oldValue + ", New value: " + newValue);
// Actual code that persists audit log
...
...
}
}
return false;
}
}