如何在休眠中为整个实体或 table 设置锁
How to set a lock for the entire entity or table in hibernate
我想在 Hibernate/Spring Data JPA 中的实体级别锁定 table。
这是我的代码的模型:
@Entity
@Data
@Table(name = "Armor")
public class Armor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OptimisticLocking
private String armorBrand;
}
除了整个 table,我可以这样做吗?
我目前正在使用 JpaRepository 进行更新。我想继续使用它而不是 EntityManager。
使用以下内容:
setLockMode(String alias, LockMode lockMode)
也许 @Lock
就是您要找的。
@Lock(LockModeType.PESSIMISTIC_READ)
public Optional<Armor> findById(Long armorId);
我想在 Hibernate/Spring Data JPA 中的实体级别锁定 table。
这是我的代码的模型:
@Entity
@Data
@Table(name = "Armor")
public class Armor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OptimisticLocking
private String armorBrand;
}
除了整个 table,我可以这样做吗?
我目前正在使用 JpaRepository 进行更新。我想继续使用它而不是 EntityManager。
使用以下内容:
setLockMode(String alias, LockMode lockMode)
也许 @Lock
就是您要找的。
@Lock(LockModeType.PESSIMISTIC_READ)
public Optional<Armor> findById(Long armorId);