如何跟踪用户对 Web 应用程序中对象的更改?
我需要帮助想出一种巧妙的方法来跟踪 Web 应用程序中的用户更改。
假设我们有这样的场景:
- 用户 X 登录并更新其个人资料中的一些字段,比如电子邮件地址。
- 这会导致用户被标记为已更新,并且应该由管理员进行检查。
- 在管理 UI 中,我们要突出显示修改的字段。
那么问题就变成了,跟踪这些更改的最佳方法是什么?管理员可能几天甚至几周都不会检查用户。我真的不需要保留原始值(至少在这一点上),我只是希望能够确定哪些字段已更改。在数据库中跟踪这一点的最佳方法是什么?
该应用程序是用 Java / JSF 编写的,使用 Hibernate 和 MySQL 进行数据访问和存储。
谢谢!
I'm needing help to come up with a clever way of keeping track of user changes in a web application.
Lets say we have this scenarion:
- User X logs in and updates a few fields in their profile, lets say the email address.
- This causes the user to be flagged as updated and it should be checked by an admin
- In the admin UI we want to highlight the modified field(s)
The question then becomes, what is the best approach to keeping track of these changes? The admin might not check the user for days or even weeks. I don't really have a need for keeping the original values (at this point at least), I just want to be able to determine what fields were changed. What would be the best way to keep track of this in the database?
The application is written in Java / JSF with Hibernate and MySQL for data access and storage.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的用例,我会选择这样的内容:
创建具有以下结构的“日志”表:
在您想要跟踪的字段上设置触发器。每当修改“记录”字段时,触发器都会创建一条指向用户和字段名称的记录。
应用程序的标记部分将允许管理员查询日志表(仅选择“未处理”记录)并一次处理一个或多个字段。
您没有提供有关标记/索引过程的许多详细信息,但该表应该足以正确识别更改的字段(并且您可以直接查询这些字段以访问当前值,这就是您所需要的)。处理(索引)给定更新后,您可以将其“已处理”标志设置为“是”(或删除它,您可以选择)。
例如,您可以选择使用时间戳字段,以便按照用户完成的顺序处理更新。
如果您甚至需要保留“以前的版本”,您只需向当前结构添加一个文本字段并更新触发器,以便它们在其中保存旧值。
Based on your use case, I'd go for something like this:
Create a "log" table with the following structure:
Set up triggers on the fields you want to keep track of. Whenever a "logged" field is modified the trigger will create a record which points to the User and the field name.
The tagging part of the application will allow the admin to query the logging table (selecting only the "non-processed" records) and process one or more field at a time.
You don't give many details on the tagging/indexing process, but this table should be enough to correctly identify the changed fields (and you can query these directly to access the current value, which is all you need). After having processed (indexed) a given update you can set its "Processed" flag to "Yes" (or delete it, your choice).
Optionally you can have a timestamp field in case you want, for example, to process your updates in the same order they were done by the users.
If you even need to keep a "previous version" you just add a text field to the current structure and update the trigger(s) so that they save the old value in it.
查看 Hibernate Envers。 -- 它提供了审核和版本控制,但我的这对于您的用例来说太胖了。
Have a look at Hibernate Envers. -- It provides Auditing and Versioning, but my this is too fat for your usecase.
您可以在数据库中创建一个用于更改的表...
如果您不需要保留旧值,则只需省略更改表中的 old_value 列即可。
You could create a table in the DB for changes...
If you don't need to keep the old values, then simply omit the old_value column in the changes table.