Monday, November 18, 2024
Google search engine
HomeLanguagesJavaHibernate – @OneToMany Annotation

Hibernate – @OneToMany Annotation

@OneToMany annotation in Hibernate is used to obtain one-to-many relationships between two entities. It is used to map a collection-valued association where a single instance of an entity is mapped to multiple instances of another entity.

Examples of @OneToMany Annotation

Example 1:

Java




// on the below line creating an entity for Section.
@Entity
public class Section {
    // on the below line creating an id for the section.
    @Id
      @GeneratedValue 
      private long id;
    // on the below line creating a field for section name.
    private String name;
    // on the below line creating an array list for the
    // students. on the below line adding annotation for
    // student list with one to many and mapping it by
    // section.
    @OneToMany(mappedBy = "section")
    private ArrayList<Student> studentList;
}
// 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
    // gender.
    private String gender;
    // on the below line creating a field for the section
    // which we are annotating with many to one annotation.
    @ManyToOne 
      private Section section;
}


Code Explanation:

In the above example we are creating two entities one is for the Section and another is for Student. In the section entity, we are creating different fields for id, name, and an array list of students which we are annotating with @OneToMany which indicates that the one section will consist of multiple students. Then we are creating an entity for Student in which we are creating different fields for id, name as well as section. We are annotating the section with @ManyToOne which indicates that multiple students will be present in one section. 

Example 2:

Java




// on the below line creating an entity for the Department.
@Entity
public class Department {
    // on the below line creating an id for the department.
    @Id 
      @GeneratedValue
      private long id;
    // on the below line creating a field for department
    // name.
    private String name;
    // on the below line creating an array list for the
    // employees. on the below line adding annotation for
    // employees list with one to many and mapping it by
    // department.
    @OneToMany(mappedBy = "department")
    private ArrayList<Employee> empList;
}
// on the below line creating an entity for Employee
@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 name;
    // on the below line creating a field for employee age
    private int age;
    // on the below line creating a field for employee
    // gender.
    private String gender;
    // on the below line creating a field for the department
    // which we are annotating with many to one annotation.
    @ManyToOne
      private Department department;
}


Code Explanation:

In the above example, we are creating two entities, one for employees and another entity for the department. In the Department, we are creating different fields such as id which we annotate with the @Id and @GeneratedValue to generate the id automatically. Then we are creating other fields such as department name and lastly create a field for the employees list present in that department. We are annotating this array list with @OneToMany and mapping it with the department. This indicates that the employees array list is mapped with a department on One to many relationship. The @OneToMany annotation also indicates that one department may consist of multiple employees. Then we create another entity for our Employees which consists of several fields such as id, name, age, gender as well as the department. We are annotating department fields with @ManyToOne which indicates that each employee is mapped to a department.

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