java - jooq RecordListener 永远不会捕获 updateEnd 和 updateStart 事件

标签 java jooq audit

使用 JOOQ 3.9.6 我尝试实现这样的记录监听器:

public class AuditListener extends DefaultRecordListener {

    ....

    @Override
    public void insertEnd(RecordContext ctx) {
        // OK we enter here when inserting a record
    }

    ...

    @Override
    public void updateEnd(RecordContext ctx) {
        // never enter this method when we update a record
    }
}

我们使用更新的 update(...) 和execute() 通过 DSLContext 更新记录。 updateEnd 事件永远不会被 AuditListener 捕获。

这是用于更新记录的存储库:

@Component
public class JooqMyentityRepository extends AbstractJooqRespository<MyentityRecord, Myentity> implements MyentityRepository {

    public JooqMyentityRepository() {
        super(Myentity.class, MYENTITY);
    }

    @Override
    public Myentity findById(Integer myentityId) {
        return dsl.select(MYENTITY.fields()).from(MYENTITY).where(MYENTITY.ID.eq((myentityId))).fetchOneInto(Myentity.class);
    }

    @Override
    @Transactional
    public void update(Myentity myentity) {
        dsl.update(MYENTITY)
            .set(MYENTITY.NAME, myentity.getName())
            .set(MYENTITY.AREA_ID, myentity.getAreaId())
            .set(MYENTITY.ACTIVE, myentity.getActive())
            .set(MYENTITY.BRAND, myentity.getBrand())
            .where(MYENTITY.ID.eq(myentity.getId()))
            .execute();

    }
}

在自定义服务中调用存储库:

@Component
@Scope(value = "singleton")
public class CustomerService {

    @Autowired
    private MyentityRepository myentityRepository;


    public void editCustomerMyentity(CustomerMyentity customerMyentity){

        Myentity myentity = myentityRepository.findbyId(customerMyentity.getId());
        ...
        // Do treatments here : insert another record to trace the modification
        ...
        myentity.setName(customerMyentity.getName());
        myentity.setBrand(customerMyentity.getBrand());
        myentityRepository.update(myentity);

    }
}

这是一个问题还是我遗漏了什么?

最佳答案

RecordListener SPI Javadoc 声明它拦截对诸如 UpdatableRecord.update() 之类的方法的调用.

它无法拦截任意更新调用,例如DSLContext.update() 甚至 DSLContext.execute("UPDATE ..") 因为 jOOQ 无法确定有多少记录以及哪些记录将受到影响数据库中的更新语句。除此之外,在您的服务中 DSLContext.update() 语句的生命周期中并不涉及实际的 UpdatableRecord,那么对 进行什么样的 SPI 调用>updateEnd() 你会期望吗?

关于java - jooq RecordListener 永远不会捕获 updateEnd 和 updateStart 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538617/

相关文章:

java - 简单的jpa问题

java - 在 PHP 中调用 Java 程序并获取输出

java - 从 jOOQ 事务访问 JDBC 连接

java - jooq 无法从 a 类转换为 b 类

asp.net - 如何审核用户何时离开 ASP.NET 应用程序

java - EclipseLink JPA 跟踪更改

c# - 在 NHibernate 中审核多对多关系

java - Android加密数据库

java - 让 jOOQ 不生成编号字段和组件

java - Apache CXF 客户端解码响应