It took me more than few minutes to find how to do one to one mapping using Hibernate Annotations, so I though I will share it.
I think Hibernate Annotations is best explained by example. Actually, I am not sure I have ever read anything about Hibernate Annotation mappings beside looking at examples…
This example is built from 2 classes: Person and PersonDetails, corresponding to 2 database tables: person and person_details. The 2 classes will demonstrate the usage of one to one mapping from both directions.
I am not sure this example is so close to the real world, but I believe it is enough to get the idea.
The person table contains the following columns:
- person_id – used as primary key with auto increment
- first_name
- last_name
- birth_date
The person_details table contains the following columns:
- person_id – used as primary key and foreign key to person table (person_id column)
- weight
- height
- hair_color
- eyes_color
Note that there may be persons with no person details. That means that having a row in person table, but no row in person_details table is a trivial case. Of course that there may be at the most one person details row for each person.
This is how the Person class looks like using Hibernate Annotations:
package com.bashan.hibernate;import org.hibernate.validator.Length;import org.hibernate.validator.NotNull;import javax.persistence.*;import java.io.Serializable;import java.util.Date;@Entity@Table(name="person")public class Person implements Serializable {@Id@GeneratedValue(strategy= GenerationType.AUTO)@Column(name="person_id")private Integer personId;@Column(name="first_name")@NotNull@Length(max = 50)private String firstName;@Column(name="last_name")@NotNull@Length(max = 50)private String lastName;@Column(name="birth_date")private Date birthDate;@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person")@JoinColumn(name="person_id")private PersonDetails personDetails;public Integer getPersonId() {return personId;}public void setPersonId(Integer personId) {this.personId = personId;}public String getFirstName() {return firstName;}public void setFirstName(String firstName) {this.firstName = firstName;}public String getLastName() {return lastName;}public void setLastName(String lastName) {this.lastName = lastName;}public Date getBirthDate() {return birthDate;}public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}public PersonDetails getPersonDetails() {return personDetails;}public void setPersonDetails(PersonDetails personDetails) {this.personDetails = personDetails;}}
And the PersonDetails class:
package com.bashan.hibernate;import org.hibernate.annotations.Cascade;import org.hibernate.annotations.GenericGenerator;import org.hibernate.annotations.Parameter;import javax.persistence.*;@Entity@Table(name="person_details")public class PersonDetails {@Id@GeneratedValue(generator="foreign")@GenericGenerator(name="foreign", strategy = "foreign", parameters={@Parameter(name="property", value="person")})@Column(name="person_id")private Integer personId;@OneToOne(fetch = FetchType.LAZY, optional=true)@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})@PrimaryKeyJoinColumnprivate Person person;@Column(name="weight")private Integer weight;@Column(name="height")private Integer height;@Column(name="hair_color")private Integer hardColor;@Column(name="eyes_color")private Integer eyesColor;public Integer getPersonId() {return personId;}public void setPersonId(Integer personId) {this.personId = personId;}public Person getPerson() {return person;}public void setPerson(Person person) {this.person = person;}public Integer getWeight() {return weight;}public void setWeight(Integer weight) {this.weight = weight;}public Integer getHeight() {return height;}public void setHeight(Integer height) {this.height = height;}public Integer getHardColor() {return hardColor;}public void setHardColor(Integer hardColor) {this.hardColor = hardColor;}public Integer getEyesColor() {return eyesColor;}public void setEyesColor(Integer eyesColor) {this.eyesColor = eyesColor;}}
That’s pretty much it… I hope it will save you time…