after looong looong and endless testing i finally figured it out :)
so i will just my solution for future reference for anyone who wants to use a shared primary key in a one to one relation ship:
the person class
Code:
@Entity
@Table(name="person")
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_seq")
@SequenceGenerator(name = "person_seq", sequenceName = "person_seq", allocationSize = 1)
@Column(name="id")
private int id;
@Column(name="name", length=40)
private String name;
@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false)
@PrimaryKeyJoinColumn(name="id", referencedColumnName="id")
private PersonAddress address;
[b] // MOST IMPORTANT
public void setAddress(PersonAddress address) {
this.address = address;
this.address.setPerson(this);
}[/b]
// other getters and setter are omitted
}
and the person_address class
Code:
public class PersonAddress implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "foreign")
@GenericGenerator(name = "foreign", strategy = "foreign", parameters = { @Parameter(name = "property", value = "person") })
@Column(name = "id")
private int id;
@OneToOne(mappedBy = "address", fetch=FetchType.LAZY)
private Person person;
@Column(name = "street")
private String street;
// getters and setter are omitted
}
there are 3 important things:
1. the person address class needs the foreign generator
2. in person when you set address the address has to know about the person... so set it right there
3. if you want a oneToOne relationship to be initiliazed lazy you HAVE to say
optional=false
well good luck to all of you :)[/b]