简体   繁体   中英

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. 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() .

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.

How is this a Java question?

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. 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

you'll have to create 2 triggers one for before the operation is performed and another after the operation is performed. 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.

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 .

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. 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. If you are using Spring instead of EJB you may advise your DAOs within application context config file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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