Hibernate One to One Example using Annotation
Here, we are going to perform one to one mapping by one-to-one element using annotation. In such case, no foreign key is created in the primary table.
In this example, one employee can have one address and one address belongs to one employee only. Here, we are using bidirectional association. Let’s look at the persistent classes.
1) Persistent classes for one to one mapping
There are two persistent classes Employee.java and Address.java. Employee class contains Address class reference and vice versa.
Employee.java
Address.java
2) Add project information and configuration in pom.xml file.
Open pom.xml file and click source. Now, add the below dependencies between <dependencies>….</dependencies> tag. These dependencies are used to add the jar files in Maven project.
3) Configuration file
This file contains information about the database and mapping file.
hibernate.cfg.xml
4) User classes to store and fetch the data
Store.java
Output
Fetch.java
Output
Download