2016-02-25 79 views
0

我有三個實體,交易者,投資組合和成員。每個交易者都有一個投資組合,而投資組合可以有許多成員。我建立了以下關係。我不確定如何使用創建的Jointable,即Portfolio_PORTFOLIOID和members_MEMBERID。很明顯,我想將每個portfolid與成員id關聯起來,但是我不知道如何去做這件事。連接數據如何持久?在JPA中堅持聯合表

Portfolio

@Entity 
@Table(name="Portfolio") 
@NamedQuery(
name="findPortfolioByTrader", 
query="SELECT p FROM Portfolio p" + 
" WHERE Trader = :trader" 
) 
public class Portfolio { 

@Id 
@GeneratedValue 
private Integer portfolioId; 
@Temporal(TIMESTAMP) 
private Date lastUpdate; 
private Integer balance; 
private Trader trader; 
private Collection<Member> members; 

public Portfolio() { 

    this.lastUpdate = new Date(); 
} 
public Portfolio(Integer balance, Trader trader) { 

    this.lastUpdate = new Date(); 
    this.balance = balance; 
    this.trader = trader; 
} 

public Integer getPortfolioId() { 
    return portfolioId; 
} 

public void setPortfolioId(Integer portfolioId) { 
    this.portfolioId = portfolioId; 
} 

public Date getLastUpdate() { 
    return lastUpdate; 
} 

public void setLastUpdate(Date lastUpdate) { 
    this.lastUpdate = lastUpdate; 
} 

@ManyToMany 
@JoinTable(
     name="MEMBER_PORTFOLIO", 
     joinColumns= 
      @JoinColumn(name="Member_MEMBERID", referencedColumnName="MEMBERID"), 
     inverseJoinColumns= 
      @JoinColumn(name="portfolio_PORTFOLIOID", referencedColumnName="PORTFOLIOID") 
) 
public Collection<Member> getMembers() { 
    return members; 
} 

public void setMembers(Collection<Member> members) { 
    this.members = members; 
} 

@OneToOne(cascade=ALL, mappedBy="portfolio") 
public Trader getTrader() 
{ 
    return trader; 
} 

public void setTrader(Trader trader) 
{ 
    this.trader = trader; 
} 

public Integer getBalance() { 
    return balance; 
} 

public void setBalance(Integer balance) { 
    this.balance = balance; 
} 


} 

Member

@Entity 
@Table(name="Member") 
@NamedQuery(
name="findAllMembers", 
query="SELECT m FROM Member m " + 
"ORDER BY m.memberId" 
) 
public class Member implements java.io.Serializable { 
/** 
* 
*/ 
private static final long serialVersionUID = -468520665316481235L; 

private String memberId; 
private String forename; 
private String surname; 
private Integer position; 
private Integer majority; 
private Integer IPO; 
private Integer questions; 
private Integer answers; 
private Party party; 
private Date lastUpdate; 
private char status; 
private Collection<Portfolio> portfolios; 
private Collection<AskOrder> askOrders; 
private Collection<BidOrder> bidOrders; 

public Member() { 

     this.lastUpdate = new Date(); 
    } 

public Member(String memberId,String forename, String surname, Integer position, 
     Integer majority, Integer IPO, Integer questions, Integer answers, Party party) { 
     this.memberId = memberId; 
     this.forename = forename; 
     this.surname = surname; 
     this.position = position; 
     this.majority = majority; 
     this.IPO = IPO; 
     this.questions = questions; 
     this.answers = answers; 
     this.party = party; 
     this.lastUpdate = new Date(); 
     this.askOrders = new ArrayList<AskOrder>(); 
     this.bidOrders = new ArrayList<BidOrder>(); 
     this.portfolios = new ArrayList<Portfolio>(); 
    } 

    @Id 
    public String getMemberId() { 
     return memberId; 
    } 

    public void setMemberId(String memberId) { 
     this.memberId = memberId; 
    } 

    public char getStatus() { 
     return status; 
    } 

    public void setStatus(char status) { 
     this.status = status; 
    } 

    @Temporal(TIMESTAMP) 
    public Date getLastUpdate() { 
     return lastUpdate; 
    } 


    public void setLastUpdate(Date lastUpdate) { 
     this.lastUpdate = lastUpdate; 
    } 

    public String getForename() 
    { 
     return forename; 
    } 

    public void setForename(String forename) 
    { 
     this.forename = forename; 
    } 

    public String getSurname() { 
     return surname; 
    } 

    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

    public Integer getPosition() { 
     return position; 
    } 

    public void setPosition(Integer position) { 
     this.position = position; 
    } 

    public Integer getMajority() { 
     return majority; 
    } 

    public void setMajority(Integer majority) { 
     this.majority = majority; 
    } 

    public Integer getIPO() { 
     return IPO; 
    } 

    public void setIPO(Integer iPO) { 
     IPO = iPO; 
    } 

    public Integer getQuestions() { 
     return questions; 
    } 

    public void setQuestions(Integer questions) { 
     this.questions = questions; 
    } 

    public Integer getAnswers() { 
     return answers; 
    } 

    public void setAnswers(Integer answers) { 
     this.answers = answers; 
    } 

    @ManyToOne 
    public Party getParty() { 
     return party; 
    } 

    public void setParty(Party party) { 
     this.party = party; 
    } 

    @OneToMany(cascade=ALL, mappedBy="member") 
    public Collection<AskOrder> getAskOrders() 
    { 
     return askOrders; 
    } 

    public void setAskOrders(Collection<AskOrder> orders) 
    { 
     this.askOrders = orders; 
    } 

    @OneToMany(cascade=ALL, mappedBy="member") 
    public Collection<BidOrder> getBidOrders() 
    { 
     return bidOrders; 
    } 

    public void setBidOrders(Collection<BidOrder> bidOrders) 
    { 
     this.bidOrders = bidOrders; 
    } 

    @ManyToMany //FIXME should probably be many to many - done 
    public Collection<Portfolio> getPortfolios() { 
     return portfolios; 
    } 

    public void setPortfolios(Collection<Portfolio> portfolios) { 
     this.portfolios = portfolios; 
    } 

} 
+1

它是一個連接表,你有沒有@Join註釋任何地方,所以不會有一個,因爲它主張。閱讀JPA文檔 –

+0

查看您的「@ManyToOne // FIXME應該可能是多對多的」評論,因爲成員只有一個Portfolio,並且不需要連接表。 – Chris

+0

謝謝你們。我已經在上面添加了一個連接表,但是我不確定從哪裏開始,因爲連接表中的數據仍然沒有保留。創建投資組合時如何創建連接表數據?或者我必須明確這麼做嗎? – zobbo

回答

0
@Entity 
    public class Portfolio 
    { 
     @Id 
     @GeneratedValue 
     private int id; 

     @ManyToMany 
     @JoinTable(name = "PortfolioMember", 
     @JoinColumns : @JoinColumn(name = "Portfolio_ID", referencedColumnName="id"), 
     @InverseJoinColumns : @JoinColumn(name = "Member_ID", referencedColumnName="id") 
    ) 
     private List<Member> members; 
    } 

    @Entity 
    public class Member 
    { 
     @Id 
     @GeneratedValue 
     private int id; 

     @ManyToMany(mappedBy = members) 
     private List<Portfolio> portfolios; 
    }