keeping the history of table in java

做~自己de王妃 提交于 2019-12-28 16:26:46

问题


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.


回答1:


If you are working with Hibernate you can use Envers to solve this problem.




回答2:


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.




回答3:


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, e.g. some database trigger executing a script, etc.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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?




回答8:


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.




回答9:


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.




回答10:


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.




回答11:


Triggers are not suggestable, when I stored my audit data in file else I didn't use the database...my suggestion is create table "AUDIT" and write java code with help of servlets and store the data in file or DB or another DB also ...



来源:https://stackoverflow.com/questions/5469231/keeping-the-history-of-table-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!