Reputation: 1131
I have two entities, a Customer and a CustomerTransaction. I want to be able to store two Customer ids within CustomerTransaction as foreign keys (one for the Customer initiating the transaction and one for the Customer receiving). I also want each Customer object to contain a list of all CustomerTransactions they are linked to.
Customer.java
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
//@ManyToOne ?
private List<CustomerTransaction> customerTransaction;
//getters and setters
}
CustomerTransaction.java
@Entity
public class CustomerTransaction {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
//@OneToMany ?
private Customer initiater;
//@OneToMany ?
private Customer receiver;
private String transactionDetails;
//getters and setters
}
How do I set up the jpa annotations so that each transaction contains a foreign key ID of the customer initiating and receiving?
Upvotes: 1
Views: 1897
Reputation: 691735
initiater
and receiver
need to be annotated with ManyToOne
(many transactions are initiated by the one initiater).
And you need two OneToMany
in Customer
: one for the initiated tranactions (OneToMany(mappedBy = "initiater")
), and one for the received transactions: (OneToMany(mappedBy = "receiver")
.
You can't have just one list (and it's probably not desirable anyway).
Upvotes: 2