Monday, September 3, 2007

Hibernate: Association Mappings in Annotation (JPA style)

Unidirectional Association

Unidirectional Association with Join Table

Bidirectional Association

Bidirectional Association with Join Table

(*) hibernate document chapter

Reference:
- hibernate documentation
- EJB 3.0 Spec


Hibernate: Annotation many-to-many (join table)

Bidirectonal many-to-many (join table) association

Hibernate Doc (Chap 8.5.3)


::Relationship::
person(many) <-> address(many)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddresses();
address.getPeople();

::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  // mapping owner
  @ManyToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @ManyToMany(mappedBy="addresses")  // map info is in person class
  private Set<Person> people;
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

- address.getPeople();
select people0_.addressId as addressId1_, people0_.personId as personId1_, person1_.personId as personId2_0_ from PersonAddress people0_ left outer join PERSON person1_ on people0_.personId=person1_.personId where people0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (join table)

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]

Hibernate: Annotation one-to-many/many-to-one (join table)

Bidirectonal one-to-many/many-to-one (join table) association

Hibernate Doc (Chap 8.5.1)


::Relationship::
person(one) <-> address(one)


::DB Schema::
person(personId)
personAddress(personId, addressId)
address(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;

  @OneToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @ManyToOne(optional=true)
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="addressId")
    },
    inverseJoinColumns = {
      @JoinColumn(name="personId")
    }
  )
  private Person person;   
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId2_, addresses0_.addressId as addressId2_, address1_.addressId as addressId3_0_, address1_1_.personId as personId4_0_, person2_.personId as personId2_1_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.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 where addresses0_.personId=?

- address.getPerson();
select address0_.addressId as addressId3_1_, address0_1_.personId as personId4_1_, person1_.personId as personId2_0_ from ADDRESS address0_ left outer join PersonAddress address0_1_ on address0_.addressId=address0_1_.addressId left outer join PERSON person1_ on address0_1_.personId=person1_.personId where address0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (primary-key)

Bidirectonal one-to-one (primary-key) association

Hibernate Doc (Chap 8.4.2)


::Relationship::
person(one) <-> address(one)


::DB Schema::
person(personId)
address(personId)


::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
  @PrimaryKeyJoinColumn
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @Column(name = "personId")
  private int id;

  @OneToOne(mappedBy="address")  // inverse=true, pointnig Person's address field
  private Person person;   
}



::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_2_, address1_.personId as personId3_0_, person2_.personId as personId2_1_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.personId=address1_.personId left outer join PERSON person2_ on address1_.personId=person2_.personId where person0_.personId=?

- address.getPerson();
select address0_.personId as personId3_2_, person1_.personId as personId2_0_, address2_.personId as personId3_1_ from ADDRESS address0_ left outer join PERSON person1_ on address0_.personId=person1_.personId left outer join ADDRESS address2_ on person1_.personId=address2_.personId where address0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (foreign-key)

Bidirectonal one-to-one (foreign-key) association

Hibernate Doc (Chap 8.4.2)


::Relationship::
person(one) <-> address(one)

::DB Schema::
person(id, addressId)
address(id)

::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;
 
  @ManyToOne
  @JoinColumn(name="addressId")     // inverse = false
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToOne(mappedBy="address")  // inverse=true, pointnig Person's address field
  private Person person;   
}



::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_1_, person0_.addressId as addressId2_1_, address1_.addressId as addressId3_0_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.addressId=address1_.addressId where person0_.personId=?

- address.getPerson();
select person0_.personId as personId2_1_, person0_.addressId as addressId2_1_, address1_.addressId as addressId3_0_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.addressId=address1_.addressId where person0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-many/many-to-one

Bidirectonal one-to-many/many-to-one association

Hibernate Doc (Chap 8.4.1)

::Relationship::
person(many) <-> address(one)
- person A and person B live in address AA

::DB Schema::
person(personId, addressId)
address(addressId)


::Java Operation::
person.getAddress();
address.getPeople();

::Annotation::

@Entity
@Table(name="PERSON")
public class Person {
   
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="personId")
  private int id;
   
  @ManyToOne
  @JoinColumn(name="addressId")     // inverse = false
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToMany(mappedBy="address")  // pointing Person's address field
  @Column(name="personId")    // inverse=true
  private Set<Person> people;
}



::Generated SQL::

[Association Mapping List]

Hibernate: Annotation many-to-many (join table)

Unidirectonal many-to-many (join table)) association

Hibernate Doc (Chap 8.3.4)


::Relationship::
person(many) -> address(many)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddresses();

::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @ManyToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}  


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;
}



::Generated SQL::
- person.getAddresses();

select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (join table)

Unidirectonal one-to-one (join table) association

Hibernate Doc (Chap 8.3.3)

::Relationship::
person(one) -> address(one)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddress();

::Annotation::

@Entity
@Table(name = "PERSON")
@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;  
}



::Generated SQL::
- person.getAddress();

select person0_.personId as personId2_1_, person0_1_.addressId as addressId3_1_, address1_.addressId as addressId4_0_ 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 where person0_.personId=?

[Association Mapping List]

Hibernate: Annotation many-to-one (join table)

Hibernate Doc (Chap 8.3.2)



::Relationship::
person(many) -> address(one)

ex) person A and person B live in address AA

::DB Schema::
person (personId)
addres (addressId)
personAddress (personId, addressId)

::Java Operation::
person.getAddress();


::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @ManyToOne(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;
}



::Generated SQL::
- person.getAddress();

select person0_.personId as personId2_1_,
person0_1_.addressId as addressId3_1_,
address1_.addressId as addressId4_0_
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
where person0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-many( join table)

Unidirectonal one-to-many (join table) association

Hibernate Doc (Chap 8.3.1)


::Relationship::
person(one) -> address(many)

::Java Operation::
person.getAddresses();


::Annotation::


@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @OneToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-many (foreign-key)

Hibernate Doc (Chap 8.2.3)

::Relationship::
person(one) -> address(many)

::DB Schema::
person( personId )
address( addressId, personId )

::Java Operation::
person.getAddresses();

::Annotation::
@Entity
@Table(name="PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;

@OneToMany
@JoinColumn(name="personId") 
private Set <Address> addresses;
}

@Entity
@Table(name = "ADDRESS")
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
}

::Generated SQL (mysql5.0)::
[Association Mapping List]