Pinaki Poddar wrote:
> 
> http://openjpa.apache.org/docs/latest/manual/manual.html#jpa_overview_mapping_assoccoll
> 

I tried that - with little success however.

Say, the object model looks like that: Person <-->* Address. The target
database layout may look like: PERSON(ID, ...), ADDRESS(ID, ...),
PERSON_ADDRESS(PERSON_ID, ADDRESS_ID).

first try:
@Entity
public class Person {
        @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
        private Set<Address> addresses = new HashSet<Address>();
}
@Entity
public class Address {
        @ManyToOne
        @JoinTable(name="PERSON_ADDRESS",
[EMAIL PROTECTED](name="ADDRESS_ID"))
        private Person person;
}
The schema looks fine. 

Loading the address collections produces incorrect SQL statement: "SELECT
t1.id, t1.city, t1.street FROM PERSON_ADDRESS t0, Address t1 WHERE
t0.PERSON_ID = ? [params=(long) 1]"

second try:
@Entity
public class Person {
        @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
        @JoinTable(name="PERSON_ADDRESS",
[EMAIL PROTECTED](name="ADDRESS_ID"))
        private Set<Address> addresses = new HashSet<Address>();
}
@Entity
public class Address {
        @ManyToOne
        private Person person;
}
org.apache.openjpa.util.MetaDataException: You have supplied columns for
"model.Person.addresses<element:class model.Address>", but this mapping
cannot have columns in this context.

third try:
@Entity
public class Person {
        @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
        @JoinTable(name="PERSON_ADDRESS",
[EMAIL PROTECTED](name="ADDRESS_ID"))
        private Set<Address> addresses = new HashSet<Address>();
}
@Entity
public class Address {
        @ManyToOne
        private Person person;
}
The schema is wrong: no join table but a join column in the Address table.

4th try:
@Entity
public class Person {
        @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
        @JoinTable(name="PERSON_ADDRESS")
        private Set<Address> addresses = new HashSet<Address>();
}
@Entity
public class Address {
        @ManyToOne
        private Person person;
}
The schema is wrong: no join table but a join column in the Address table.

5th try (secondary table):
@Entity
public class Person {
        @OneToMany(mappedBy="person", cascade=CascadeType.ALL)
        private Set<Address> addresses = new HashSet<Address>();
}
@Entity
@SecondaryTable(name="PERSON_ADDRESS",
[EMAIL PROTECTED](name="ADDRESS_ID"))
public class Address {
        @ManyToOne
        @JoinColumn(table="PERSON_ADDRESS")
        private Person person;
}
The schema is OK, however loading the address-collection still leads to the
incorrect SQL statement.

I am out of ideas.

-- Frank

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

Reply via email to