Java Mailing List Archive

http://www.gg3721.com/

Home » users.openjpa »

Re: bidirectional one-to-many relationship with join-table

Frank Schwarz

2008-08-07

Replies: Find Java Web Hosting

Author LoginPost Reply


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",
joinColumns=@(protected)"))
 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",
inverseJoinColumns=@(protected)"))
 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",
joinColumns=@(protected)"))
 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",
pkJoinColumns=@(protected)"))
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

--
Sent from the OpenJPA Users mailing list archive at Nabble.com.

©2008 gg3721.com - Jax Systems, LLC, U.S.A.