Bidirectonal one-to-one (join table) association (very unusual)
Hibernate Doc (Chap 8.5.2)
::Relationship::
person(one)(mapping owner) <-> address(one)
::DB Schema::
person(
personId)
address(
addressId)
personaddress(personId, addressId)
::Java Operation::
person.getAddress();
address.getPerson();
::Annotation::
@Entity
@Table(name = "PERSON")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "personId")
private int id;
@OneToOne(optional=true)
@JoinTable(name="PersonAddress",
joinColumns = {
@JoinColumn(name="personId", unique = true)
},
inverseJoinColumns = {
@JoinColumn(name="addressId")
}
)
private Address address;
}
@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
@OneToOne(optional=true, mappedBy="address") // pointing to Person's address field
private Person person;
}
::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_2_, person0_1_.addressId as addressId3_2_, address1_.addressId as addressId4_0_, address1_1_.personId as personId3_0_, person2_.personId as personId2_1_, person2_1_.addressId as addressId3_1_ from PERSON person0_ left outer join PersonAddress person0_1_ on person0_.personId=person0_1_.personId left outer join ADDRESS address1_ on person0_1_.addressId=address1_.addressId left outer join PersonAddress address1_1_ on address1_.addressId=address1_1_.addressId left outer join PERSON person2_ on address1_1_.personId=person2_.personId left outer join PersonAddress person2_1_ on person2_.personId=person2_1_.personId where person0_.personId=?
- address.getPerson();
select address0_.addressId as addressId4_2_, address0_1_.personId as personId3_2_, person1_.personId as personId2_0_, person1_1_.addressId as addressId3_0_, address2_.addressId as addressId4_1_, address2_1_.personId as personId3_1_ from ADDRESS address0_ left outer join PersonAddress address0_1_ on address0_.addressId=address0_1_.addressId inner join PERSON person1_ on address0_1_.personId=person1_.personId left outer join PersonAddress person1_1_ on person1_.personId=person1_1_.personId left outer join ADDRESS address2_ on person1_1_.addressId=address2_.addressId left outer join PersonAddress address2_1_ on address2_.addressId=address2_1_.addressId where address0_.addressId=?
[Association Mapping List]