Hi,
  Please try the following mapping for a bi-directional one-to-many relation
with a join table.
 
public class Person {
  @Id
  private long ssn;
        
  @OneToMany(cascade=CascadeType.ALL)
  @JoinTable(name="PERSON_ADDRESSES",
      joinColumns           = @JoinColumn(name="PERSON_SSN",     
referencedColumnName="SSN"),
      inverseJoinColumns = @JoinColumn(name="ADDRESS_PHONE",
referencedColumnName="PHONE"))
   private Set<Address> addresses = new HashSet<Address>();

public class Address {
  @Id
  private String phone;
        
  @ManyToOne
  @JoinColumn(table="PERSON_ADDRESSES", referencedColumnName="SSN")
  private Person person;


The above mapping manifests to following database schema in MySQL

CREATE TABLE ADDRESS (
  phone varchar(255) NOT NULL,
  PRIMARY KEY  (phone)
)

CREATE TABLE PERSON (
  `ssn` bigint(20) NOT NULL,
  PRIMARY KEY  (`ssn`)
) 

and a Join Table
CREATE TABLE PERSON_ADDRESSES (
  ADDRESS_PHONE varchar(255) default NULL,
  PERSON_SSN       bigint(20) default NULL,
  KEY I_PRSNSSS_ADDRESS_PHONE (ADDRESS_PHONE),
  KEY I_PRSNSSS_PERSON             (PERSON_SSN)
) 

Let us know if this works for you and you can perform CRUD operations on
this schema.

-- 
View this message in context: 
http://n2.nabble.com/bidirectional-one-to-many-relationship-with-join-table-tp678479p681198.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to