相关疑难解决方法(0)

带有 Spring 数据 JPA 的数据库悲观锁(幕后休眠)

我需要一些悲观实体锁定方面的帮助。我在我的应用程序中使用 PostgreSQL 和 Spring 数据 JPA(引擎盖下的 hibernate 5)。所以,我想展示一个我面临的任务。

我有一些有钱的用户帐户:

@lombok.Data //Used to generate getters and setters
@Entity
class AccountEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private Long balance;
}
Run Code Online (Sandbox Code Playgroud)

和付款,允许将钱从一个帐户转移到另一个帐户

@lombok.Data
@Entity
class PaymentEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private Long amount;

    @OneToOne
    @JoinColumn(name="account_from_id")
    private AccountEntity accountFrom;

    @OneToOne
    @JoinColumn(name="account_to_id")
    private AccountEntity accountTo;
}
Run Code Online (Sandbox Code Playgroud)

两者的存储库:

interface AccountRepository extends JpaRepository<AccountEntity, Integer> {}

interface PaymentRepository extends JpaRepository<PaymentEntity, Integer> {}
Run Code Online (Sandbox Code Playgroud)

以及进行汇款的服务:

interface PaymentService {
    PaymentEntity create(PaymentEntity paymentEntity);
}

@Service …
Run Code Online (Sandbox Code Playgroud)

spring hibernate jpa locking spring-data-jpa

5
推荐指数
0
解决办法
3369
查看次数

标签 统计

hibernate ×1

jpa ×1

locking ×1

spring ×1

spring-data-jpa ×1