keeping the history of table in java

后端 未结 11 1687
误落风尘
误落风尘 2020-12-10 09:54

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条回答
  • 2020-12-10 10:45

    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?

    0 讨论(0)
  • 2020-12-10 10:48

    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.

    0 讨论(0)
  • 2020-12-10 10:52

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

    0 讨论(0)
  • 2020-12-10 10:52

    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.

    0 讨论(0)
  • 2020-12-10 10:52

    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.

    0 讨论(0)
提交回复
热议问题