pb00067, now I think, that I had to have a mind blackout ;]
The same functionality (described in my initial post) can be gained by regular @OneToOne Hibernate mapping.
If some is interested - I will give my example.
Relation between Person and Office is: 1 to {0 or 1}
There a 3 classes:
* PersonPk
* Person
* Office
Code:
import javax.persistence.Column;
public class PersonPk implements Serializable
{
private static final long serialVersionUID = 7494175680933460258L;
private String firstName;
private String lastName;
PersonPk(){}
public PersonPk(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
@Column(name="first_name")
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
@Column(name="last_name")
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
}
Class Person:
Code:
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "PERSON")
public class Person
{
private PersonPk pk;
private String zodiac;
private Office office;
@EmbeddedId
public PersonPk getPk(){
return pk;
}
public void setPk(PersonPk pk){
this.pk = pk;
}
@OneToOne(optional=true, fetch=FetchType.EAGER)
@PrimaryKeyJoinColumn
public Office getOffice(){
return office;
}
public void setOffice(Office office){
this.office = office;
}
@Column
public String getZodiac(){
return zodiac;
}
public void setZodiac(String zodiac){
this.zodiac = zodiac;
}
}
Class Office:
Code:
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
@Entity
@Table(name = "OFFICE")
public class Office
{
private PersonPk pk;
private Person person;
private String address;
@EmbeddedId
public PersonPk getPk(){
return pk;
}
public void setPk(PersonPk pk){
this.pk = pk;
}
@OneToOne(optional=false, fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
public Person getPerson(){
return person;
}
public void setPerson(Person person){
this.person = person;
}
@Column(name="address")
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
}
Take care!
Adam Woźniak / Poland