title: Persisting a Joint Table in JPA
body: <p>I have three entities, Trader, Portfolio and Member. Each Trader has a Portfolio and a Portfolio can have many Members. I have set up the following relationships. I'm not sure how to use the Jointable that is created, i.e. Portfolio_PORTFOLIOID and members_MEMBERID. Obviously I'd like to associate each portfolid with member id's, however I'm not sure how to go about this. How is the jointable data persisted?</p>
  
  <p>My <code>Portfolio</code> class</p>
  
  <pre><code>@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&lt;Member&gt; 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&lt;Member&gt; getMembers() {
      return members;
  }
  
  public void setMembers(Collection&lt;Member&gt; 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;
  }
  
  
  }
  </code></pre>
  
  <p>My <code>Member</code> class</p>
  
  <pre><code>@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&lt;Portfolio&gt; portfolios;
  private Collection&lt;AskOrder&gt; askOrders;
  private Collection&lt;BidOrder&gt; 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&lt;AskOrder&gt;();
          this.bidOrders = new ArrayList&lt;BidOrder&gt;();
          this.portfolios = new ArrayList&lt;Portfolio&gt;();
      }
  
      @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&lt;AskOrder&gt; getAskOrders() 
      {
          return askOrders;
      }
  
      public void setAskOrders(Collection&lt;AskOrder&gt; orders)
      {
          this.askOrders = orders;
      }
  
      @OneToMany(cascade=ALL, mappedBy="member")
      public Collection&lt;BidOrder&gt; getBidOrders() 
      {
          return bidOrders;
      }
  
      public void setBidOrders(Collection&lt;BidOrder&gt; bidOrders)
      {
          this.bidOrders = bidOrders;
      }
  
      @ManyToMany //FIXME should probably be many to many - done
      public Collection&lt;Portfolio&gt; getPortfolios() {
          return portfolios;
      }
  
      public void setPortfolios(Collection&lt;Portfolio&gt; portfolios) {
          this.portfolios = portfolios;
      }
  
  }
  </code></pre>