in JPA, Spring

JPA – Associations @OneToOne , @OneToMany, @ManyToOne, @ManyToMany part 1

JPA entities are usually related to other entities. There are in total 4 types of associations

  • @OneToOne association is used when one entity has only one other entity that it relates to. Example of this association is Employee and EmployeeDetail. Each employee has( or can have) only one detail.
  • @OneToMany, is used when entity is related to many entities. Example is Company that has many Departments.
  • @ManyToOne is opposite from previous association. In this case Department is related to one Company.
  • @ManyToMany, example of this association is a Teacher that can tech many students and Student that can be thought by many teachers.

@OneOnOne

To demonstrate this association I will use example where Employee has EmployeeDetail.

  • @Entity annotates this class as JPA entity
  • @Table specifies table name, in this case EMPLOYEE
  • Primary column as identity is specified using using @Id and @GeneratedValue
  • @Column specifies column name, length and is null among with other properties that can be specified.

In above entities there are two @OneToOne annotated properties to define both sides of the association, owning and inverse side.

Questions that we need to answer are:

  • Foreign key is going to be created in which table?
  • Who is the owner if the association?
  • If one entity is deleted is other entity going to be deleted as well?

Looking to above configuration foreign key is going to be created in table that is related to entity EmplyeeDetail. This is specified using  @JoinColumn(name = EMPLOYEE_ID_) , name also specifies the name of the column in the database table.

Owner of association is EmployeeDetail entity. Not owning( or inverse) side must have mappedBy attribute in its OneToOne annotation.

When Employee is deleted EmployeeDetail should be deleted as well. This is specified by cascade being located in Employee entity.

UnitTests

To make sure this works I have created two JpaRepository interfaces that are used.

CrudRepository interface has CRUD methods that are going to be used in unit test.

What is important to note is when creating Employee and EmployeeDetail both sides of association  must be maintained.

Cascade Delete Test

First I will load both employee and employeeDetail from the database to prove that they both exist. Of course both entities are created in before method that you can see above.

Then I delete the employee and check did this delete employeeDetail as well.

Why was the EmployeeDetail deleted?

It is deleted because of cascade = CascadeType.ALL specified in Employee entity. It is important to thing before so that you do not automatically remove wrong entities.

 

Write a Comment

Comment

Webmentions

  • JPA – Associations @OneToOne , @OneToMany, @ManyToOne, @ManyToMany part 2 – pscode.rs

    […] previous post I wrote about @OneOnOne association, now I will go trough oneToMany and manyToOne associations with […]