简体   繁体   English

在Java中保留表格的历史记录

[英]keeping the history of table in java

I need the sample program in Java for keeping the history of table if user inserted, updated and deleted on that table. 我需要Java中的示例程序来保留表的历史记录(如果用户在该表上插入,更新和删除了该表)。 Can anybody help in this? 有人可以帮忙吗?

Thanks in advance. 提前致谢。

如果您使用的是Hibernate ,则可以使用Envers解决此问题。

You have two options for this: 您有两种选择:

  1. Let the database handle this automatically using triggers. 让数据库使用触发器自动处理此问题。 I don't know what database you're using but all of them support triggers that you can use for this. 我不知道您正在使用哪个数据库,但是它们都支持可用于此的触发器。
  2. Write code in your program that does something similar when inserting, updating and deleting a user. 在您的程序中编写代码,以在插入,更新和删除用户时执行类似的操作。

Personally, I prefer the first option. 就个人而言,我更喜欢第一种选择。 It probably requires less maintenance. 它可能需要较少的维护。 There may be multiple places where you update a user, all those places need the code to update the other table. 您可能在多个地方更新用户,所有这些地方都需要代码来更新另一个表。 Besides, in the database you have more options for specifying required values and integrity constraints. 此外,在数据库中,您还有更多用于指定所需值和完整性约束的选项。

触发器是不建议使用的,当我将审计数据存储在文件中时,否则我没有使用数据库...我的建议是创建表“ AUDIT”并在Servlet的帮助下编写Java代码,并将数据存储在文件或DB或其他文件中DB也...

Well, we normally have our own history tables which (mostly) look like the original table. 好吧,我们通常有自己的历史记录表,(大多数情况下)看起来像原始表。 Since most of our tables already have the creation date, modification date and the respective users, all we need to do is copy the dataset from the live table to the history table with a creation date of now() . 由于大多数表已经具有创建日期,修改日期和相应的用户,因此我们要做的就是将创建日期为now()的数据集从实时表复制到历史表。

We're using Hibernate so this could be done in an interceptor, but there may be other options as well, eg some database trigger executing a script, etc. 我们正在使用Hibernate,因此可以在拦截器中完成,但是可能还有其他选项,例如某些数据库触发器执行脚本等。

How is this a Java question? 这是Java问题吗?

This should be moved in Database section. 这应该在数据库部分中移动。

You need to create a history table. 您需要创建一个历史表。 Then create database triggers on the original table for "create or replace trigger before insert or update or delete on table for each row ...." 然后在原始表上创建数据库触发器,以便“在插入或更新或删除表中的每一行之前创建或替换触发器...。”

I think this can be achieved by creating a trigger in the sql-server. 我认为可以通过在sql-server中创建触发器来实现。 you can create the TRIGGER as follows: 您可以按以下方式创建触发器:

Syntax: 句法:

CREATE TRIGGER trigger_name {BEFORE | 创建触发器触发器名称{ AFTER } {INSERT | 之后} {插入| UPDATE | 更新| DELETE } ON table_name FOR EACH ROW triggered_statement DELETE} ON table_name每行触发的语句

you'll have to create 2 triggers one for before the operation is performed and another after the operation is performed. 您将必须创建2个触发器,一个在执行操作之前创建,而另一个在执行操作之后创建。 otherwise it can be achieved through code also but it would be a bit tedious for the code to handle in case of batch processes. 否则它也可以通过代码来实现,但是在批处理过程中,代码要处理起来会有些繁琐。

You should try using triggers. 您应该尝试使用触发器。 You can have a separate table (exact replica of your table of which you need to maintain history) . 您可以有一个单独的表(需要维护其历史记录的表的精确副本)。

This table will then be updated by trigger after every insert/update/delete on your main table. 然后,在主表上进行每次插入/更新/删除操作后,将通过触发器来更新该表。

Then you can write your java code to get these changes from the second history table. 然后,您可以编写Java代码以从第二个历史记录表中获取这些更改。

I think you can use the redo log of your underlying database to keep track of the operation performed. 我认为您可以使用基础数据库的重做日志来跟踪执行的操作。 Is there any particular reason to go for the program? 参加该计划有什么特别的理由吗?

You could try creating say a List of the objects from the table (Assuming you have objects for the data). 您可以尝试从表中创建一个对象列表(假设您有数据对象)。 Which will allow you to loop through the list and compare to the current data in the table? 哪一个可以让您遍历列表并与表中的当前数据进行比较? You will then be able to see if any changes occurred. 然后,您将能够查看是否发生了任何更改。

You can even create another list with a object that contains an enumerator that gives you the action (DELETE, UPDATE, CREATE) along with the new data. 您甚至可以使用包含枚举器的对象创建另一个列表,该枚举器为您提供操作(删除,更新,创建)以及新数据。

Haven't done this before, just a idea. 以前没有做过,只是一个想法。

Like @Ashish mentioned, triggers can be used to insert into a seperate table - this is commonly referred as Audit-Trail table or audit log table . 就像提到的@Ashish一样,触发器可用于插入单独的表中-通常称为Audit-Trail表Audit Log表

Below are columns generally defined in such audit trail table : 'Action' (insert,update,delete) , tablename (table into which it was inserted/deleted/updated), key (primary key of that table on need basis ) , timestamp (the time at which this action was done) 以下是此类审计跟踪表中通常定义的列:“操作”(插入,更新,删除),表名(插入/删除/更新表的表),键( 根据需要该表的主键),时间戳(执行此操作的时间)

It is better to audit-log after the entire transaction is through. 最好在整个事务完成后审核日志。 If not, in case of exception being passed back to code-side, seperate call to update audit tables will be needed. 如果不是这样,则在将异常传递回代码端的情况下,将需要单独调用更新审核表。 Hope this helps. 希望这可以帮助。

If you are talking about db tables you may use either triggers in db or add some extra code within your application - probably using aspects. 如果您在谈论数据库表,则可以在数据库中使用触发器,也可以在应用程序中添加一些额外的代码-可能使用方面。 If you are using JPA you may use entity listeners or perform some extra logic adding some aspect to your DAO object and apply specific aspect to all DAOs which perform CRUD on entities that needs to sustain historical data. 如果您使用的是JPA,则可以使用实体侦听器或执行一些额外的逻辑,以向DAO对象添加某些方面,并将特定方面应用于所有对需要维持历史数据的实体执行CRUD的DAO。 If your DAO object is stateless bean you may use Interceptor to achive that in other case use java proxy functionality, cglib or other lib that may provide aspect functionality for you. 如果您的DAO对象是无状态Bean,则可以使用Interceptor来实现在其他情况下使用Java代理功能,cglib或其他可以为您提供方面功能的lib。 If you are using Spring instead of EJB you may advise your DAOs within application context config file. 如果您使用的是Spring而不是EJB,则可以在应用程序上下文配置文件中建议DAO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM