2017-03-16 133 views
1

我有兩個實體,一個Customer和一個CustomerTransaction。我希望能夠將CustomerTransaction中的兩個Customer ID存儲爲外鍵(一個用於發起交易的客戶,另一個用於客戶接收)。我還希望每個Customer對象都包含它們鏈接到的所有CustomerTransaction的列表。來自同一實體的多個外鍵? (JPA休眠)

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 
} 

如何設置了JPA註解,使每一筆交易包含客戶發起和接收的外鍵ID?

回答

1

initiaterreceiver需要註解ManyToOne(許多交易是由一個啓動者發起的)。

而且你需要在:一個用於啓動tranactions(OneToMany(mappedBy = "initiater")),以及一個用於接收交易:(OneToMany(mappedBy = "receiver")

你不能只是一個列表(而且它可能不希望反正)。

+0

謝謝你的幫助! – DraegerMTN