如何在 JPA/Hibernate 中概念化 @OneToMany 映射

How to conceptualize @OneToMany mapping in JPA/Hibernate

我刚开始学习 JPA 和 Hibernate,我对理解 reading/conceptualizing @OneToMany 在 JPA 和 Hibernate 中映射的正确方法感到困惑。

举个例子,假设一个账户可以有很多交易,但一个交易只能链接到一个账户。

所以我把这个观点读给 "Account class/Entity" 为“一个帐户可以有很多交易,因此我应该将交易字段标记为@OneToMany。

以及 "Transaction class/Entity" 的其他视角,我认为 "One Transaction can only be linked to One Account thus I should mark Account field in the Transaction Entity as @OneToOne"

但是从我观看的几乎所有视频教程和网络资源中都提到交易实体的帐户字段应该是@ManyToOne,因为"Many Transaction can be linked with one Account"这是真的,但对我来说,这与我们在账户实体以不同的方式解释,但没有说 "One Transaction can only be linked to one Account"

关系的另一方

如果你能帮助我理解如何阅读和如何,为什么当一个实体是@OneToMany时对面的关系。

@Entity
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long accountId;

    @OneToMany(cascade = CascadeType.ALL)
    private List<Transaction> transactions = new ArrayList<>();
}
@Entity
public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long transactId;

    @ManyToOne
    Account account;
}

我喜欢这样想:

@Entity
public class Account {
    // Here I read this as "One Account (The entity we are in) ToMany Transactions (The foreign entity)
    @OneToMany(cascade = CascadeType.ALL)
    private List<Transaction> transactions = new ArrayList<>();
}
@Entity
public class Transaction {
    // If we use the same process as above we get:
    // Many Transactions (The entity we are in) ToOne Account (The foreign entity)
    @ManyToOne
    Account account;
}