我正在用SpringBoot和SpringDataJPA构建一个银行应用程序,在定义bankaccount类和banktransfer类之间的关系时遇到了一些问题,我找不到解决方案。银行账户有一个转账清单 List<BankTransfer> transfers
. 所以这里的关系被定义为@onetomany。但在每一笔银行转账中,我都有两个银行账户参考,分别是“发送方”和“接收方”。发送方是从中提取金额的帐户,接收方是添加金额的帐户。
因此,每个银行转账都必须与bankaccount类有@manytone关系(这种关系正好是二对一关系)。所以这里的难点是如何Map两个银行账户(发送方和接收方)?在bankaccount类的@onetomany注解中设置什么作为mappedby字段。
以下是我的课程:
bankaccount类:
@Entity
@Table(name = "bank_account")
public class BankAccount {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_Sequence")
@SequenceGenerator(name = "account_Sequeence", sequenceName = "BANK_ACCOUNT_SEQ")
@Column(name ="id")
private Long id;
@NotNull
@Column(name ="account_number")
private String accountNumber;
@NotNull
@Enumerated
@Column(name ="type")
private AccountType type;
@Column(name ="currency")
private Currency currency;
@NotNull
@Column(name ="balance")
private BigDecimal balance;
@NotNull
@Column(name ="date_created")
protected Date dateCreated;
@ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name="client_id")
protected Client client;
@Column(name ="employee_id")
private Employee accountManager;
@OneToMany(fetch = FetchType.LAZY,
mappedBy = "sender",
cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
protected List<BankTransfer> transfers;
}
银行转账类:
@Entity
@Table(name="transfer")
public class BankTransfer {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "transaction_Sequence")
@SequenceGenerator(name = "transaction_Sequeence", sequenceName = "TRANSACTION_SEQ")
private Long id;
@NotNull
@Column(name = "amount")
private BigDecimal amount;
@NotNull
@ManyToOne(targetEntity = BankAccount.class,
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "sender_id")
private BankAccount sender;
@NotNull
@ManyToOne(targetEntity = BankAccount.class,
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "receiver_id")
private BankAccount receiver;
@NotNull
@Column(name = "created_at")
private Date createdAt;
}
如你所见,我有:
@OneToMany(fetch = FetchType.LAZY,
mappedBy = "sender",
cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
protected List<BankTransfer> transfers;
在bankaccount类中尝试Map BankAccount sender
banktransfer类中的字段:
@NotNull
@ManyToOne(targetEntity = BankAccount.class,
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name = "sender_id")
private BankAccount sender;
我还要Map BankAccount receiver
banktransfer类中的字段。我试过这个例子:
mappedBy = {"sender", "receiver"},
或者尝试添加2@onetomany注解,但这些似乎是错误的。
你能帮我解决这个问题吗。也许我需要分别重新定义实体和oracle数据库表。我对rdbms比较陌生,我正在构建这个应用程序以获得更多的使用经验。
1条答案
按热度按时间cu6pst1q1#
这取决于如何填充这些实体上的数据。如果您创建新的数据库,您可以
join table
使用@OneToMany
.一般来说,在你这样的情况下,我只使用relation
BankTransfer
这样你就可以从BankAccount
尤其是你有偷懒的关系。从实体转移BankAccount
可以通过sql、jpa等实现,实体中不需要字段