EclipseLink 审计/历史/跟踪变更 [英] EclipseLink Audit / History / Track changes

查看:28
本文介绍了EclipseLink 审计/历史/跟踪变更的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实现一种方法来跟踪数据更改并为我的应用程序创建历史日志.因为我正在使用 EclipseLink,所以它应该很容易并且有可能像他们 写EclipseLink 常见问题解答第一个解决方案有效,但基于第二个事件的解决方案无效.每次引发事件时,ObjectChangeSet 为空.

i've tried to implement a way to track data changes and create a history log for my application. Because i'm using EclipseLink it should be easy and possible to get the changes like they write on the EclipseLink FAQ The first solution works but the second event based won't work. Everytime the event is raised the ObjectChangeSet is null.

我不是简单地使用 HistoryPolicy 的原因是我不想将有关登录用户(而不是 db 用户)的信息和更改的数据存储到单独的表中.我搜索了很多,但找不到任何解决此问题的方法.

The reason why i'm not simply using HistoryPolicy is that i wan't to store information about the logged on user (not the db user) and the changed data to a separate table. I searched a lot but can't find any solution to this problem.

这是我的实体类:

@Entity
@EntityListeners(HistoryEventListener.class)
@Table(name = "t_users")
    public class Users implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GENSEQ_USERS")
@SequenceGenerator(name = "GENSEQ_USERS", sequenceName = "SEQ_USERS", allocationSize = 1, initialValue = 1)
private Integer id;
@Column(nullable = false)
private String userid;
...
}

这是我的 DescriptorEventAdapter 类:

And this is my DescriptorEventAdapter class:

public class HistoryEventListener extends DescriptorEventAdapter {

@Override
public void postUpdate(DescriptorEvent event) {
    ObjectChangeSet changeSet = event.getChangeSet();
    if (changeSet != null) {
        System.out.println("ObjectChangeSet not null");
    }
    System.out.println("ObjectChangeSet null");
}

@Override
public void postMerge(DescriptorEvent event) {
    ObjectChangeSet changeSet = event.getChangeSet();
    if (changeSet != null) {
        System.out.println("ObjectChangeSet not null");
    }
    System.out.println("ObjectChangeSet null");
}
}

我使用过的persistence.xml文件:

The persistence.xml file which i've used:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="PhoneBookPU" transaction-type="RESOURCE_LOCAL">
    <provider>
        org.eclipse.persistence.jpa.PersistenceProvider
    </provider>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@somehostname:1521:xyz"/>
        <property name="javax.persistence.jdbc.password" value="getyourown"/>
        <property name="javax.persistence.jdbc.user" value="username"/>
        <property name="javax.persistence.logging.level" value="INFO"/>
    </properties>
</persistence-unit>
</persistence>

任何帮助将不胜感激.

推荐答案

看起来更改集只是为合并事件设置的,而不是为 postUpdate 设置的.请记录错误以将更改集添加到更新前/更新后事件.

Looks like the change set is only set for the merge event, not the postUpdate. Please log a bug to have the change sets added to the pre/postUpdate events.

您还可以从查询或对象中获取 ChangeSet,

You can also get the ChangeSet from the query or object,

((UpdateObjectQuery)event.getQuery()).getObjectChangeSet()

或者,

((AttributeChangeListener)((ChangeTracker)event.getObject())._persistence_getPropertyChangeListener()).getObjectChangeSet()

这篇关于EclipseLink 审计/历史/跟踪变更的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆