使用 Java 和 Hibernate 存储历史数据

发布于 2024-10-05 09:55:26 字数 1072 浏览 0 评论 0原文

这是历史数据处理的问题。 假设您有一个如下所示的类 MyClass:

class MyClass {
    String field1;
    Integer field2;
    Long field3;

    getField1() {...}
    setField1(String ...) {...}

    ...
}

现在,假设我需要使 MyClass 能够存储和检索旧数据,那么最好的方法是什么?
要求也是通过 Hibernate 保留这些类。 每个“实体”最多有两个表:只有一个表或一个表用于“连续性”类(代表随时间演变的实体),另一个表用于历史数据(正如此处建议的那样)
请注意,我必须能够为字段的值分配任意有效时间。

该类应该具有如下接口:

class MyClass {
    // how to store the fields????

    getField1At(Instant i) {...}
    setField1At(Instant i, String ...) {...}

    ...
}

我当前正在使用 JTemporal 库,并且它有一个 TemporalAttribute 类,它就像一个地图:您可以执行诸如 T myAttr.get(Instant i) 之类的操作来获取 Instant i 处 myAttr 的版本。我知道如何使用 Hibernate 将 TemporalAttribute 保存在表中(很简单:我保存 TemporalAttribute 使用的 SortedMap,然后您将获得一个包含开始和结束有效时间以及属性值的表)。
真正的问题是这里我们有多个属性。
我心里有一个解决方案,但还不清楚,我想听听您的想法。

This is a problem about historical data handling.
Suppose you have a class MyClass like the following one:

class MyClass {
    String field1;
    Integer field2;
    Long field3;

    getField1() {...}
    setField1(String ...) {...}

    ...
}

Now, suppose I need to make MyClass able to store and retrieve old data, what's the best way to do this?
The requirements are to persist the classes through Hibernate, too. And to have at most two tables per "entity": only one table or one table for the "continuity" class (the one which represents the entity which evolves over the time) and another table for the historical data (as it's suggested here)
Please note that I have to be able to assign an arbitrary valid time to the values of the fields.

The class should have an interface like:

class MyClass {
    // how to store the fields????

    getField1At(Instant i) {...}
    setField1At(Instant i, String ...) {...}

    ...
}

I'm currently using the JTemporal library, and it has a TemporalAttribute<T> class, which is like a map: you can do things like T myAttr.get(Instant i) to get the version of myAttr at Instant i. I know how to persist a TemporalAttribute in a table with Hibernate (it's simple: I persist the SortedMap used by the TemporalAttribute and you get a table with start and end valid time and the value of the attribute).
The real problem is that here we have multiple attributes.
I have a solution in mind but it's not clear, and I'd like to hear your ideas.

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

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

发布评论

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

评论(2

不寐倦长更 2024-10-12 09:55:26

你的项目让我想起了Hibernate Envers

Envers 项目旨在轻松实现
持久化类的审计。全部
你要做的就是注释你的
持久化类或者它的一些类
您想要审核的属性,
与@Audited。对于每一个经过审核的
实体,将创建一个表,其中
将保存所做更改的历史记录
到实体。然后您可以检索
并查询历史数据,无需太多
努力。

  • 选择您想要审核的内容(基于每个属性)
  • 创建您自己的修订实体(存储诸如修订号、作者、时间戳等信息)

使用 Hibernate Envers 来实现实体和修订数据的解耦(在数据库和您的数据库中)代码)。

Your project reminds me of Hibernate Envers.

The Envers project aims to enable easy
auditing of persistent classes. All
that you have to do is annotate your
persistent class or some of its
properties, that you want to audit,
with @Audited. For each audited
entity, a table will be created, which
will hold the history of changes made
to the entity. You can then retrieve
and query historical data without much
effort.

  • choose what you want to audit (on a per attribute basis)
  • make your own Revision Entity (that stores informations such as revision number, author, timestamp...)

Using Hibernate Envers for this decouples entities and revision data (in database and in your code).

合久必婚 2024-10-12 09:55:26

您只需向域类添加版本号即可完成类似的操作。我做了类似的事情,其中​​ Id 是数据库分配的编号和版本号之间的组合,但我建议不要这样做。使用普通的代理键,如果您确实需要,请将 [id, version] 元组设置为自然键。

实际上,您可以通过这种方式对整个对象图进行版本控制,只需确保图上所有元素的版本号相同即可。然后您可以轻松返回到任何以前的版本。

您应该编写大量服务测试以确保管理版本的代码的完整性。

You can do something like this simply by adding a version number to your domain class. I did something like this where the Id was a composite between an db assigned number and the version number, but I would advise against that. Use a normal surrogate key, and if you really want, make the [id, version] tuple a natural key.

You can actually version entire object graphs that way, just by ensuring that the version number is the same for all elements on the graph. You can then easily go back to any previous version.

You should write a lot of service tests to insure the integrity of the code that manages the version.

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