假设我有以下实体:
public class Transfer {
...
private BankAccount senderAccount;
private BankAccount receiverAccount;
...
}
并且
public class BankAccount {
...
private List<Transfer> transfers;
...
}
我希望BankAccount类中的转移列表能够同时保存,发送和接收转移。是否可以创建如下注释:
@OneToMany(mappedBy = 'senderAccount', mappedBy = 'receiverAccount')
如果没有,最好的方法是什么?
编辑:我希望发送和接收的传输都存储在一个列表中。
谢谢
答案 0 :(得分:0)
当然你写它时不能拥有它(编译错误)。解决方案很简单:
public class BankAccount {
...
@OneToMany(mappedBy = 'receiverAccount')
private List<Transfer> transfersAsReceiver;
@OneToMany(mappedBy = 'senderAccount')
private List<Transfer> transfersAsSender;
...
}
答案 1 :(得分:0)
你能做的是:
public enum TransferDirection {
SENDER,
RECEIVER;
}
public class Transfer {
private TransferDirection direction;
private BankAccount account;
...
}
因此,您要存储一个传输对象,该传输对象由枚举区分,将其标识为发送方或接收方。