JavaDream JPA JPA Mapping Short Notes

JPA Mapping Short Notes

In JPA( Java persistence API) mapping automatically map the java objects to the database tables. Here we are providing JPA Mapping Short Notes. In JPA there are mainly 4 types of relationship.

  • One-to-One Mapping
  • One-to-Many Mapping
  • Many-to-One Mapping
  • Many-to-Many Mapping

We generally heard the terms Unidirectional mapping and Bidirectional mapping. Let’s see what does the mean of these two terms.

Unidirectional Mapping

In JPA unidirectional mapping means we have a mapping from one way only. Let’s understand it by an example, Say we have two tables, First one is Employee and the second is Email and we want to do one-to-one mapping. So if we are doing one-to-one mapping it means a Employee can have one email-id and each email-id must be associated with only one employee.

As we know unidirectional mapping is of one way, It means from Employee table we can get the associated email-id detail but from Email table we can’t find the associated employee information from Employee table.

Bidirectional Mapping

Bidirectional mapping is just reverse of the unidirectional mapping. In bidirectional mapping we have mapping in two ways. Like in above example while we are using unidirectional mapping we can get the email-id details from Employee table but vice-versa is not true.

But when we use bidirectional mapping, mapping ‘ll be of two ways. Means we can get the email-id details from Employee table and also get the employee details from the Email table.

When we use bidirectional mapping we mostly heard the terms owning side and non-owning side (non-owning side also called inverse side). So let’s understand about owning and non-owning side.

Owning vs Non-Owning Side

Owning side means table that contains the foreign key is called the owner or owning side table. In this Employee and Email table example. Email table is the owning side as it contains the foreign key.

Non-Owning or inverse side is the table that refer to the owning table. In this example Employee is the non-owning table as it doesn’t contains the foreign key but it refers to the Email table that contains the foreign key.

How to create Bidirectional Mapping

The next question comes into the mind is how can we create the bidirectional mapping. JPA provide the keyword mappedBy for creating the bidirectional mapping. We always use mappedBy keyword on non-owning side(table that does not contain the foreign key). Along with mappedBy keyword we use the @joinColumn keyword on the owning side of the table. Below is the syntax of mappedBy

@Entity
@Table(name = "employee")
public class Employee {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private String name;

	@OneToOne(mappedBy = "employee", cascade = CascadeType.ALL, orphanRemoval = true)
	private Email email;

        // Getter and Setter

}Code language: PHP (php)

@joinColumn is used to create the foreign key in the table. It’s always use on the owning side table. Below is the syntax of @joinColumn

@Entity
@Table(name = "email")
public class Email {

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	private String emailId;

	@OneToOne
	@JoinColumn(name = "employeeId_fk", referencedColumnName = "id")
	private Employee employee;
         
        // Getter and Setter
}Code language: PHP (php)

After running the code 2 tables creates Employee and Email. Employee table contains two column id and name, whereas email table contains three columns(id, email_Id, employee_id_fk). Here employee_id_fk is the foreign key.

While using mappedBy keyword we have to make sure that foreign key attribute must have the same name that we provided in the mappedBy=”” property, As in this example we use mappedBy=”employee” so in Email class Employee class variable must be employee( private Employee employee ). Please refer below image for reference.

Here we provide JPA Mapping Short Notes for More in-depth knowledge please refer official docs of JPA: https://docs.oracle.com/javaee/6/tutorial/doc/bnbpz.html

Learn More on JPA

Spring boot H2 Database Example

Share with others

Related Post