Suresh Rohan's Blog

This blog is all about Java, J2EE,Spring, Angular, React JS, NoSQL, Microservices, DevOps, BigData, Tutorials, Tips, Best practice, Interview questions, Views, News, Articles, Techniques, Code Samples, Reference Application and much more

Wednesday, July 10, 2013

Hibernate One-to-One Mappings by example using JPA annotation

One-to-One Mappings


unidirectional One-to-One Mappings


  • A more realistic example of a one-to-one association, however, would be an employee who has a parking space. Assuming that every employee got his or her own parking space, then we would create a one-to-one relationship from Employee to ParkingSpace



  • We define the mapping in a similar way to the way we define a many-to-one mapping, except that we use the @OneToOne annotation on the parkingSpace attribute instead of a @ManyToOne annotation. 
  • Just as with a many-to-one mapping, the one-to-one mapping has a join column in the database and needs to override the name of the column in an @JoinColumn annotation when the default name does not apply. 
  • The default name is composed the same way as for many-to-one mappings using the name of the source attribute and the target primary key column name.


  • the tables mapped by the Employee and ParkingSpace entities. The foreign key column in the EMPLOYEE table is named PSPACE_ID and refers to the PARKING_SPACE table.
  • As it turns out, one-to-one mappings are almost the same as many-to-one mappings except
  • for the fact that only one instance of the source entity is able to refer to the same target entity instance. 
  • In other words, the target entity instance is not shared amongst the source entity instances. In the database, this equates to having a uniqueness constraint on the source foreign key column (that is, the foreign key column in the source entity table). If there were more than one foreign key value that was the same, then it would contravene the rule that no more than one source entity instance can refer to the same target entity  instance.


One-to-One Relationship from Employee to ParkingSpace

@Entity
public class Employee {
@Id private int id;
private String name;
@OneToOne
@JoinColumn(name="PSPACE_ID")
private ParkingSpace parkingSpace;
// ...
}

Bidirectional One-to-One Mappings

  • To make our existing one-to-one employee and parking space example bidirectional, we need only change the ParkingSpace to point back to the Employee.



Inverse Side of a Bidirectional One-to-One Relationship

@Entity
public class ParkingSpace {
@Id private int id;
private int lot;
private String location;
@OneToOne(mappedBy="parkingSpace")
private Employee employee;
// ...
}

  • it assumes that Employee is the owning side of the 
    relationship. We now have to add a reference from ParkingSpace back to Employee. This is 
    achieved by adding the @OneToOne relationship annotation on the employee field. 
  • As part of the 
    annotation we must add a mappedBy element to indicate that the owning side is the Employee and not the ParkingSpace. Because ParkingSpace is the inverse side of the relationship, it does not have to supply the join column information.
The two rules, then, for bidirectional one-to-one associations are:
  1. The @JoinColumn annotation goes on the mapping of the entity that is mapped to the table containing the join column, or the owner of the relationship. This may be on either side of the association.
  2. The mappedBy element should be specified in the @OneToOne annotation in the entity that does not define a join column, or the inverse side of the relationship.

One-to-One Primary Key Mappings

  • A specific case of a unique one-to-one relationship is when the primary keys of the related entities are always guaranteed to match. 
  • The two entities must always have been created with the same identifiers, and in the database each primary key could also be used as a foreign key to the other entity. 
  • Of course the direction in which the actual constraint occurs should dictate which side is the owner since the owner is the one with the foreign key constraint in its table.


  • Imagine if every time an employee got hired, his or her employee id was used as their parking space id (and employees were never allowed to change parking spaces!). This relationship would be modeled in the database
  • The difference between mapping this kind of one-to-one relationship and the previous kind is that there is no additional foreign key column in either table. The primary key in the PARKING_SPACE table is also a foreign key to the EMPLOYEE table. When this is the case, then an @PrimaryKeyJoinColumn is used instead of an @JoinColumn annotation. 
  • Because the foreign key is now defined from ParkingSpace to Employee, we need to reverse the ownership of the relationship and make ParkingSpace the owner. We specify the @PrimaryKeyJoinColumn annotation on the employee field of the ParkingSpace entity.

One-to-One Primary Key Relationship

@Entity
public class Employee {
@Id private int id;
private String name;
@OneToOne(mappedBy="employee")
private ParkingSpace parkingSpace;
// ...
}
@Entity
public class ParkingSpace {
@Id private int id;
private int lot;
private String location;
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
// ...
}


No comments:

Post a Comment