在java中保存表的历史记录

发布于 2024-10-24 10:05:25 字数 71 浏览 2 评论 0原文

如果用户在该表上插入、更新和删除,我需要 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

凶凌 2024-10-31 10:05:25

如果您正在使用 Hibernate 您可以使用 Envers 来解决这个问题。

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

苏辞 2024-10-31 10:05:25

为此,您有两种选择:

  1. 让数据库使用触发器自动处理此问题。我不知道您正在使用什么数据库,但它们都支持您可以用于此目的的触发器。
  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.

时光与爱终年不遇 2024-10-31 10:05:25

嗯,我们通常有自己的历史表,它(大部分)看起来像原始表。由于我们的大多数表已经具有创建日期、修改日期和各自的用户,因此我们需要做的就是将数据集从实时表复制到创建日期为 now() 的历史表。

我们使用 Hibernate,因此这可以在拦截器中完成,但也可能有其他选项,例如某些数据库触发器执行脚本等。

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.

静若繁花 2024-10-31 10:05:25

这是一个Java问题吗?

这应该移到数据库部分。

您需要创建一个历史表。然后在原始表上创建数据库触发器,用于“在表上为每一行插入、更新或删除之前创建或替换触发器......”

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

谁许谁一生繁华 2024-10-31 10:05:25

我认为这可以通过在sql-server中创建触发器来实现。
您可以按如下方式创建触发器:

语法:

创建触发器触发器名称
{之前|之后} {插入|更新 |
DELETE } ON 表名 FOR EACH ROW
触发语句

您必须创建 2 个触发器,一个在执行操作之前使用,另一个在执行操作之后使用。
否则也可以通过代码来实现,但是在批处理的情况下代码处理起来会有点乏味。

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.

白鸥掠海 2024-10-31 10:05:25

您应该尝试使用触发器。您可以拥有一个单独的表(您需要维护其历史记录的表的精确副本)。

在主表上的每次插入/更新/删除后,该表将由触发器更新。

然后您可以编写 Java 代码以从第二个历史表中获取这些更改。

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.

浪推晚风 2024-10-31 10:05:25

我认为您可以使用底层数据库的重做日志来跟踪所执行的操作。参加这个项目有什么特别的理由吗?

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?

不乱于心 2024-10-31 10:05:25

您可以尝试创建表中的对象列表(假设您有数据对象)。这将允许您循环遍历列表并与表中的当前数据进行比较?然后您将能够查看是否发生了任何更改。

您甚至可以使用包含枚举器的对象创建另一个列表,该枚举器为您提供操作(删除、更新、创建)以及新数据。

以前没有这样做过,只是一个想法。

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.

木有鱼丸 2024-10-31 10:05:25

就像@Ashish提到的那样,触发器可用于插入到单独的表中 - 这通常称为审计跟踪表审计日志表

以下是此类审计跟踪表中通常定义的列:“操作”(插入、更新、删除)、表名(插入/删除/更新的表)、键(该表的主键根据需要< /em>) 、时间戳(完成此操作的时间)

最好在整个事务完成后进行审核日志。如果没有,在异常被传递回代码端的情况下,将需要单独调用来更新审计表。希望这有帮助。

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.

﹏雨一样淡蓝的深情 2024-10-31 10:05:25

如果您正在谈论数据库表,您可以在数据库中使用触发器或在应用程序中添加一些额外的代码 - 可能使用方面。如果您使用 JPA,您可以使用实体侦听器或执行一些额外的逻辑,向 DAO 对象添加某些方面,并将特定方面应用于对需要维护历史数据的实体执行 CRUD 的所有 DAO。如果您的 DAO 对象是无状态 bean,您可以使用 Interceptor 来实现,在其他情况下使用 java 代理功能、cglib 或其他可以为您提供方面功能的库。如果您使用 Spring 而不是 EJB,您可以在应用程序上下文配置文件中建议您的 DAO。

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.

野稚 2024-10-31 10:05:25

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文