在 Hibernate Envers 中获取实体的先前版本
我有一个由 Hibernate 加载的实体(通过 EntityManager):
User u = em.load(User.class, id)
该类由 Hibernate Envers 审核。 如何加载用户实体的先前版本?
I have an entity loaded by Hibernate (via EntityManager
):
User u = em.load(User.class, id)
This class is audited by Hibernate Envers. How can I load the previous version of a User entity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是另一个版本,它可以查找相对于“当前”修订版号的先前修订版,因此即使您正在查看的实体不是最新修订版,也可以使用它。 它还可以处理没有先前修订的情况。 (
em
被假定为先前填充的 EntityManager)这可以概括为:
这种概括的唯一棘手的部分是获取实体的 id。 因为我正在使用 Play! 框架中,我可以利用所有实体都是模型的事实并使用
((Model)entity).id
来获取 id,但您必须调整它以适合您的环境。Here's another version that finds the previous revision relative to a "current" revision number, so it can be used even if the entity you're looking at isn't the latest revision. It also handles the case where there isn't a prior revision. (
em
is assumed to be a previously-populated EntityManager)This can be generalized to:
The only tricky bit with this generalization is getting the entity's id. Because I'm using the Play! framework, I can exploit the fact that all entities are Models and use
((Model) entity).id
to get the id, but you'll have to adjust this to suit your environment.也许是这样(来自 AuditReader 文档)
(我对此很陌生,不确定我的语法是否正确,也许 size()-1 应该是 size()-2?)
maybe this then (from AuditReader docs)
(I'm very new to this, not sure if I have all the syntax right, maybe the size()-1 should be size()-2?)
我想应该是这样的:
I think it would be this:
基于 @brad-mace 的出色方法,我进行了以下更改:
所以这是另一个解决方案:
Building off of the excellent approach of @brad-mace, I have made the following changes:
So here's another solution:
来自文档:
From the docs: