JOOQ - 相当于用于填充历史字段的hibernate拦截器? [英] JOOQ - equivalent of hibernate interceptor for populating history fields?

查看:405
本文介绍了JOOQ - 相当于用于填充历史字段的hibernate拦截器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:
Spring应用程序,使用JOOQ 3.7,使用Postgres作为我的数据库,自动从模式生成JOOQ映射代码。



我是将一些代码从Hibernate移植到JOOQ。
Hibernate代码在每个上下文的基础上提取一些身份验证细节,以填充createdBy,updatedBy,日期等字段。



我目前只能通过JOOQ看到这样做的方法是开发人员必须记住编写代码以便在每次更新对象时手动更新字段,我可以看到它会很乏味且容易忘记填充字段。



使用JOOQ,我是否有办法以更好的方式处理每个表上的历史字段,而不是手工编写一堆代码? / p>

我们目前不使用DAO,我宁愿避免编写/生成整个代码层来处理这些历史字段。



另一个选择可能是在数据库中执行此操作,如果JOOQ无法帮助解决问题,那么这可能就是我们要做的。

解决方案

使用jOOQ有几种方法可以做到这一点:



1。使用 RecordListener UpdatableRecord上生成这些值



每次调用以下任何一个时都会调用 RecordListener SPI:





但是,当您编写明确的DML时,不会调用此SPI声明。有关详细信息,请参阅手册,此处:





2。使用 VisitListener 转换jOOQ生成的所有SQL语句



VisitListener SPI用于任意SQL转换操作。您可以截取由jOOQ生成的各种SQL语句,并为它们添加其他子句,例如

  UPDATE表SET a = 1,b = 2 WHERE id = 3 

将成为

  UPDATE表SET a = 1,b = 2,updatedBy ='me'WHERE id = 3 

此SPI记录在手册中:





< H3> 3。写入触发器

我认为最好的解决方案是使用触发器将这种自动生成的数据移动到数据库中。这将允许您在迁移,手动更新或通过Java之外的其他语言(例如Perl脚本等)访问时更新这些值。



4。等待jOOQ 3.10



jOOQ 3.10有待处理的功能请求:





它将实现这一功能,以及其他一些不错的补充,例如二维版本控制:




Environment: Spring application, using JOOQ 3.7, generating the JOOQ mapping code automatically from the schema, using Postgres as my database.

I've been porting some code from Hibernate to JOOQ. The Hibernate code pulls some authentication details stashed away on a per context basis to populate fields like "createdBy", "updatedBy", dates, etc.

The only way I can see to do this with JOOQ at the moment is that developers would have to remember to write code to update the fields by hand every time they update an object and I can see it's going to be tedious and easy to forget to populate the fields.

Using JOOQ, is there some way I can deal with "history fields" on each table in a better way than writing a bunch of code by hand?

We don't use DAOs at the moment, I'd prefer to avoid writing/generating an entire layer of code just to deal with these history fields.

Another option might be to do this in the database, and if JOOQ can't help with the problem, that may well be what we'll do.

解决方案

There are several ways to do this with jOOQ:

1. Use a RecordListener to generate these values on UpdatableRecord

The RecordListener SPI is invoked every time you call any of:

However, this SPI is not invoked when you write explict DML statements. For more information, see the manual, here:

2. Use a VisitListener to transform all the SQL statements generated by jOOQ

The VisitListener SPI is there for arbitrary SQL transformation operations. You can intercept all sorts of SQL statements as generated by jOOQ, and add additional clauses to them, e.g.

UPDATE table SET a = 1, b = 2 WHERE id = 3

Would become

UPDATE table SET a = 1, b = 2, updatedBy = 'me' WHERE id = 3

This SPI is documented here in the manual:

3. Write triggers

The best solution in my opinion is to move this kind of auto-generated data into the database by using triggers. This will allow you to update these values also in the event of a migration, manual update, or access via another language than Java (e.g. a Perl script, etc.)

4. Wait for jOOQ 3.10

There's a pending feature request for jOOQ 3.10:

It will implement precisely this feature, along with a couple of other nice additions, such as two-dimensional versioning:

这篇关于JOOQ - 相当于用于填充历史字段的hibernate拦截器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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