-1

Assume I have the following entities:

public class Transfer {

...

private BankAccount senderAccount;
private BankAccount receiverAccount;

...

}

And

public class BankAccount {

...

private List<Transfer> transfers;

...

}

I want the transfers list inside my BankAccount class to hold both, sent and received transfers. Will it be possible to create an annotation like:

@OneToMany(mappedBy = 'senderAccount', mappedBy = 'receiverAccount')

If not, what will be the best approach to this?

EDIT: I want both sent and received transfers to be stored in one List.

Thank you

2 Answers2

0

Of course you cannot have it as you wrote it (compile error). The solution is simple:

public class BankAccount {

...
@OneToMany(mappedBy = 'receiverAccount')
private List<Transfer> transfersAsReceiver;

@OneToMany(mappedBy = 'senderAccount')
private List<Transfer> transfersAsSender;
...

}
V G
  • 18,822
  • 6
  • 51
  • 89
  • Is it possible to hold all transfers in one list? – Engineering Machine Feb 13 '15 at 13:27
  • Not directly. You could add a property `getAllTransfers()` that [joins](http://stackoverflow.com/questions/7177004/adding-two-arraylists-into-one) both lists: `transfersAsReceiver` and `transfersAsSender`. – V G Feb 13 '15 at 13:28
0

What you could do is:

public enum TransferDirection {
    SENDER,
    RECEIVER;
}

public class Transfer {

private TransferDirection direction;

private BankAccount account;

...

}

So you are storing one transfer object which is distinguished by the enum which identifies it as sender or receiver.

kism3t
  • 1,343
  • 1
  • 14
  • 33