Locking

要指定要使用的锁定模式,您可以在查询方法上使用 @Lock 注解,如下例所示:

To specify the lock mode to be used, you can use the @Lock annotation on query methods, as shown in the following example:

Example 1. Defining lock metadata on query methods
interface UserRepository extends Repository<User, Long> {

  // Plain query method
  @Lock(LockModeType.READ)
  List<User> findByLastname(String lastname);
}

该方法声明会导致触发查询配备 LockModeTypeREAD。您还可以在您的存储库接口中重新声明 CRUD 方法并添加 @Lock 注解,从而为 CRUD 方法定义锁定,如下例所示:

This method declaration causes the query being triggered to be equipped with a LockModeType of READ. You can also define locking for CRUD methods by redeclaring them in your repository interface and adding the @Lock annotation, as shown in the following example:

Example 2. Defining lock metadata on CRUD methods
interface UserRepository extends Repository<User, Long> {

  // Redeclaration of a CRUD method
  @Lock(LockModeType.READ)
  List<User> findAll();
}