You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Frank Schwarz <fr...@gmx.de> on 2008/05/15 15:20:30 UTC
One-sided one-to-many - how to map?
Hi,
my apologies if this question has been asked recently. However my search
did not reveal an appropriate answer.
How do I map a unidirectional one-to-many relationship between a master
and a detail such that a column "master_id" in table "detail" is used
for joining both tables?
According to [1], it should be like:
@Entity
public class Person {
@OneToMany
@ElementJoinColumn(name="Person_id")
private Set<Address> addresses = new HashSet<Address>();
}
@Entity
public class Address {
}
However, this is causing two issues:
1.) I have to do the schema creation on my own as the OpenJPA mapping
tool creates a join-table database mapping, i.e.
CREATE TABLE Address (id NUMBER NOT NULL, ..., PRIMARY KEY (id));
CREATE TABLE Person (id NUMBER NOT NULL, ..., PRIMARY KEY (id));
CREATE TABLE Person_Address (Person_id NUMBER, addresses_id NUMBER);
2.) I cannot transfer an address-object from one person to another:
Address address = person1.getAddresses().iterator().next();
person1.getAddresses().remove(address);
person2.getAddresses().add(address);
This raises an InvalidStateException: Attempt to set column
"Address.Person_id" to two different values: (null)"null", (class
java.lang.Long)"2".
Is the mapping correct? And if so, how can I get the mapping tool to
create the correct table definitions?
Kind regards,
Frank
[1]
http://openjpa.apache.org/builds/1.0.2/apache-openjpa-1.0.2/docs/manual/manual.html#ref_guide_mapping_jpa_onemany