- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
有一个实体 Foo
带有 @Version
列。如果我想删除它,我希望 Spring Data JPA 和/或 Hibernate 检查 @Version
列的当前值是否与数据库中的值匹配。如果不是,删除应该被拒绝。这与分离实体的预期一样有效:
@Transactional
public void delete(Foo foo) {
fooRepository.delete(foo); // throws ObjectOptimisticLockingFailureException
}
但是,如果我先从存储库加载实体,然后使用不同版本在同一事务中删除它,则无论 @Version
列的值如何,删除都会通过:
@Transactional
public void delete(int fooId, long version) {
Foo foo = fooRepository.findOne(fooId);
foo.setVersion(version);
fooRepository.delete(foo); // passes regardless of value of version
}
当我查看 Hibernate 调试输出时,执行了版本比较(delete from foo where id=? and version=?
)但没有达到我预期的效果。
我错过了什么?
最佳答案
来自JPA specification , 3.4.2 部分:
An entity may access the state of its version field or property or export a method for use by the application to access the version, but must not modify the version value. With the exception noted in section 4.10, only the persistence provider is permitted to set or update the value of the version attribute in the object.
version 属性的目的是防止我们在当前持久化上下文中加载对象后可能发生的并发更新,Hibernate 通过忽略您手动设置的任何值来实现它,而是使用从加载对象时的数据库。为了验证这一点,enable还打印绑定(bind)变量值,您会注意到使用了数据库中的值。
例如,在使用 DTO 时实际使用的标准解决方案是在从 DTO 更新实体状态时手动执行检查:
if (entity.getVersion() != dto.getVersion()) {
throw new OptimisticLockException("...");
}
当然,您可以通过扩展一个基类来使它更通用,该基类为所有版本可用的实体提供此检查,或者在某些 util 方法中。比如有的作者直接在version setter中做:
public void setVersion(long version) {
if (this.version != version) {
throw new OptimisticLockException("...");
}
}
Hibernate 会自动为分离的实体执行此检查,如 DefaultMergeEventListener
的实现中所示。 :
else if (isVersionChanged(entity, source, persister, target)) {
if (source.getFactory().getStatistics().isStatisticsEnabled()) {
source.getFactory().getStatisticsImplementor()
.optimisticFailure(entityName);
}
throw new StaleObjectStateException(entityName, id);
}
关于java - Spring 数据 JPA : Delete Optimistic Locking semantics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43779279/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!