Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaHibernate – @OneToOne Annotation

Hibernate – @OneToOne Annotation

@OnetoOne annotation in Hibernate is used to create a one-to-one association between two entities. The one-to-one annotation indicates that one instance of an entity is associated with only one instance of the other entity. When we annotate a field or method with @Onetoone annotation then Hibernate will create the one-to-one relation between these two entities. We can use @OnetoOne annotation on either side of the association depending on the directionality of the relationship.

Examples for @OneToOne Annotation

Example 1:

Java




// on the below line creating an entity for student
@Entity
public class Student {
    // on the below line creating an id for student which is
    // generated value.
    @Id @GeneratedValue private long id;
    // on the below line creating a field for the student
    // name.
    private String name;
    // on the below line creating a field for student
    // education details adding one to one annotation to map
    // students with education.
    @OneToOne(mappedBy = "student")
    private Education education;
}
// on the below line creating an entity
// for education details of students.
@Entity
public class Education {
    // on the below line creating an id
    // for student education details.
    @Id 
    @GeneratedValue
    private long id;
    // on the below line creating a field for ssc
    // percentage.
    private int sscPercentage;
    // on the below line creating a field for hssc
    // percentage.
    private int hscPercentage;
    // on the below line mapping education with students
    // by using one to one annotation.
    @OneToOne
    @JoinColumn(name = "id")
    private Student student;
}


Code Explanation:

In the above example, the Student entity is having a one-to-one relationship with the Education entity. The student entity has the fields as student name, id, and student education. The student entity is the owner of this relationship as it is indicated by the mappedBy attribute in the @OnetoOne association on the education field. 

The Education entity is not the owner of this relationship. It uses @JoinColumn annotation to specify the foreign key column name id that references the primary key of the Student entity.  With @OnetoOne annotation, Hibernate will automatically create the foreign key constraint in the database to enforce this one-to-one relationship between these two entities.

Example 2:

Java




@Entity
public class Employee {
    // on the below line creating an id for an employee
    // which is generated value.
    @Id
      @GeneratedValue
      private long id;
    // on the below line creating a field for employee name.
    private String employeeName;
    // on the below line creating a field for employee
    // address details adding one to one annotation to map
    // employees with address.
    @OneToOne(mappedBy = "employee")
    private Address address;
}
// on the below line creating an entity for the address
// details of the employee.
@Entity
public class Address {
    // on the below line creating an id for employee address
    // details.
    @Id
      @GeneratedValue
      private long id;
    // on the below line creating a field for a flat number.
    private int flatNumber;
    // on the below line creating a field for the street.
    private String street;
    // on the below line creating a field for the city.
    private String city;
    // on the below line creating a field for state
    private String state;
    // on the below line creating a field for pincode
    private int pincode;
    // on the below line mapping address with employee by
    // using one to one annotation.
    @OneToOne
    @JoinColumn(name = "id")
    private Employee employee;
}


Code Explanation:

In the above example, we are creating an Employee entity that consists of different fields such as id which is annotated with @Id and @GeneratedValue to generate the id automatically. After that, we are creating other fields for employee entities such as employee name and address. We are annotating the address field with @OnetoOne annotation and mapping it with the employee. 

Then we are creating an entity for Address in which we are creating a field for id which is annotated with @Id and @GeneratedValue to generate the id automatically. After that, we are creating different fields for flat number, street, city, state, and PIN code for the address. Then we are creating an employee field which we are annotating with One to one to create a one-to-one association between employee and address entities.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments