如何在 JPA/Hibernate 中概念化 @OneToMany 映射 | 珊瑚贝

How to conceptualize @OneToMany mapping in JPA/Hibernate


我刚开始学习 JPA

  • 嗯,一个账户可以有很多交易。所以它是一对多的。因此,逆只能是多对一的:许多交易可以有相同的帐户。如果是一对一,对方也是一对一,一个账户只能有一笔交易。这是不正确的。
  • @JBNizet 所以这意味着所有 OneToMany 关系只有 MayToOne 在他们的另一边/实体?
  • 是的,它是对称的。一对多-多对一;一对一-一对一;多对多-多对多。


我喜欢这样想:

1
2
3
4
5
6
7
8
9
10
11
12
13
@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;
}

来源:https://www.codenong.com/58342375/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?