如何在jparepository save上执行悲观锁

mo49yndu  于 2021-07-16  发布在  Java
关注(0)|答案(0)|浏览(228)

当我在同一个实体上同时保存时,我得到一个 ObjectOptimisticLockingFailureException :
org.springframework.orm.objectoptimisticlockingfailureexception:标识符为[…]的类[…myentity]的对象:乐观锁定失败;嵌套异常为org.hibernate.staleobjectstateexception:行被另一个事务更新或删除(或未保存的值Map不正确)
悲观主义者如何锁定日元汇率 save() 方法以避免此异常?
代码段:

@Entity
@Audited
public class PersonEntity extends BaseEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private UUID id;

    @Version
    @Column(nullable = false)
    private Long entityVersion;
}

@Repository
public interface MyEntityRepository extends JpaRepository<MyEntity, UUID> {
    @Override
    @Lock(LockModeType.PESSIMISTIC_WRITE)
    PersonEntity save(PersonEntity personEntity);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题