何时在Hibernate中使用@Version和@Audited? [英] When to use @Version and @Audited in Hibernate?

查看:908
本文介绍了何时在Hibernate中使用@Version和@Audited?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以在Hibernate中使用 @Version @Audited 这个用例吗?

解决方案

@Version用于实现乐观锁定与Hibernate,这意味着没有两个事务在冲突的同时覆盖数据。

如果数据被两个线程同时读取,并且都尝试使用不同的值更新同一行,
Hibernate使用 @Version 字段检查该行是否已经更新。

在提交之前,每个事务都会验证没有其他事务修改了其数据。
如果修改,最后一次事务遇到处理陈旧数据错误。

@Audited 用于执行关于Hiberate实体部分的审计功能 Envers


Could someone help me with the use case when to use @Version and @Audited in Hibernate?

解决方案

@Version is used to implement Optimistic locking with Hibernate, which means that no two transactions override the data at the same time with a conflict.
If the data is read by two threads at the same time, and both try to update the same row with different values, Hibernate uses the @Version field to check if the row is already updated.
Before committing, each transaction verifies that no other transaction has modified its data. If modified, the last transaction encounters a "Working with stale data" error.

@Audited is used to perform auditing functionality on entities part of Hiberate Envers

这篇关于何时在Hibernate中使用@Version和@Audited?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆