keeping the history of table in java

2019-01-18 17:12发布

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.

11条回答
The star\"
2楼-- · 2019-01-18 18:06

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

查看更多
放我归山
3楼-- · 2019-01-18 18:08

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

查看更多
虎瘦雄心在
4楼-- · 2019-01-18 18:09

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.

查看更多
小情绪 Triste *
5楼-- · 2019-01-18 18:11

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-18 18:12

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.

查看更多
登录 后发表回答