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:

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.
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;
// ...
}





0 comments



10

Jul

Hibernate Many-to-One Mappings Tutorial by example
Filed under ( Hibernate ) by Suresh Rohan

Many-to-One Mappings







In our cardinality discussion of the Employee and Department relationship , we first thought of an employee working in a department, so we just assumed that it was a one to- one relationship.
However, when we realized that more than one employee works in the same department, we changed it to a many-to-one relationship mapping. It turns out that many-to-one is the most common mapping and is the one that is normally used when creating an association to an entity.




we show a many-to-one relationship between Employee and Department. Employee is the “many” side and the source of the relationship, and Department is the “one” side and the target. Once again, because the arrow points in only one direction, from Employee to Department, the relationship is unidirectional.
Note that in UML, the source class has an implicit attribute of the target class type if it can be navigated to. For example, Employee has an attribute called department that will contain a reference to a single Department instance.
A many-to-one mapping is defined by annotating the attribute in the source entity (the attribute that refers to the target entity) with the @ManyToOne annotation



Many-to-One Relationship from Employee to Department


@Entity
public class Employee {
// ...
@ManyToOne
private Department department;
// ...
}



we can see how the @ManyToOne annotation is used to map this relationship. The department field in Employee is the source attribute that is annotated.


Using Join Columns



In the database, a relationship mapping means that one table has a reference to another table. The database term for a column that refers to a key (usually the primary key) in another table is a foreign key column.
In the Java Persistence API we call them join columns, and the @JoinColumn annotation is the primary annotation used to configure these types of columns.




Consider the EMPLOYEE and DEPARTMENT tables, that correspond to the Employee and Department entities. The EMPLOYEE table has a foreign key column named DEPT_ID that references the DEPARTMENT table.
From the perspective of the entity relationship, DEPT_ID is the join column that associates the Employee and Department entities.
For example, the @JoinColumn(name="DEPT_ID") annotation means that the DEPT_ID column in the source entity table is the foreign key to the target entity table, whatever the target entity of the relationship happens to be.

Many-to-One Relationship Overriding the Join Column


@Entity
public class Employee {
@Id private int id;
@ManyToOne
@JoinColumn(name="DEPT_ID")
private Department department;
// ...
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -