[英]What is the best approach to implement a versioning feature with content comparison using Hibernate?
I'm working on a JSF based web application with backing beans and Hibernate for persistence. 我正在使用基于JSF的Web应用程序,该应用程序具有支持bean和Hibernate的持久性。
The goal is to implement a feature to compare changes across versions, where some attributes are from "foreign key" objects. 目标是实现一种功能,以比较各个版本之间的更改,其中某些属性来自“外键”对象。 In order to get a generic solution, the best way would be to create a kind of database trigger or something. 为了获得通用的解决方案,最好的方法是创建一种数据库触发器或类似的东西。
I know about the @Version
annotation, but I don't want only to have a history about the persisted object, but also the associated object. 我知道@Version
批注,但我不仅想拥有关于持久化对象的历史,而且也不想拥有有关的对象的历史。
Anyone already done something like that? 有人已经做过类似的事情吗?
Edit: To specify the question here is more detail information. 编辑:此处指定问题是更多详细信息。 The application have a lot of business objects, most of them just have simple text attributes like Strings. 该应用程序有很多业务对象,其中大多数只具有简单的文本属性,例如字符串。 The main thing the application do is CRUD. 应用程序要做的主要事情是CRUD。 I would like to build a feature where users can compare versions older versions of a business object. 我想构建一个功能,用户可以比较业务对象的旧版本。 My first idea was just to have a version column in the tables of the business objects and to save always a new dataset with a new version. 我的第一个想法只是在业务对象的表中有一个version列,并始终使用新版本保存新的数据集。 But this seems for me like a large overload, to save everything duplicated. 但是对我来说,这似乎是一个很大的负担,可以保存所有重复的内容。 And how to keep the associations between versioned objects? 以及如何保持版本化对象之间的关联? Is there an other, lighter aprroach for that? 还有其他更轻巧的方法吗? This approach would also imply to change every single database access class and save method. 这种方法还意味着更改每个数据库访问类和保存方法。
I would solve the problem like this: 我会解决这样的问题:
business object table, example: EXAMPLE_TABLE
业务对象表,例如: EXAMPLE_TABLE
----------------------------------- |ID |column 1|column 2|...| |---------------------------------- |1 |foo |bar |...| |2 |baz |test |...| |... |... |... |...| -----------------------------------
history preservation table for each business object table, example: EXAMPLE_TABLE_HISTORY
每个业务对象表的历史记录保留表,例如: EXAMPLE_TABLE_HISTORY
------------------------------------------------ |ID |column 1|column 2|...|version|date| |----------------------------------------------- |1 |null |foo |...|1 | | |1 |baz |test |...|2 | | |... |... |... |...|... |... | ------------------------------------------------
You can define database-triggers for EXAMPLE_TABLE
on each operation (INSERT, UPDATE or DELETE) to copy the current data from EXAMPLE_TABLE
to EXAMPLE_TABLE_HISTORY
including a version (Column version
) and a timestamp (Column date
) to preserve a history. 您可以在每个操作(INSERT,UPDATE或DELETE)上为EXAMPLE_TABLE
定义数据库触发,以将当前数据从EXAMPLE_TABLE
复制到EXAMPLE_TABLE_HISTORY
其中包括版本(列version
)和时间戳(列date
)以保存历史记录。
Advantages: 好处:
ID
column in the history preservation table. 您可以使用历史记录保存表中的ID
列轻松检索业务对象的所有版本。 I think I found what I'm looking for. 我想我找到了想要的东西。 Event though I wanted to solve it with db triggers, hibernate provides a pretty solution. 事件虽然我想用数据库触发器来解决,但是休眠提供了一个很好的解决方案。 Hibernate envers creates an audit table for each versioned/annotated entity. Hibernate envers为每个版本化/带注释的实体创建一个审核表。 All you have to do is to set the annotations in auditable entities. 您要做的就是在可审核的实体中设置注释。 The needed tables will be generated and also the history of related audited entities will be managed. 将会生成所需的表,还将管理相关审计实体的历史记录。 So you can recreate the whole content of your entities. 因此,您可以重新创建实体的全部内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.