Hibernate - @OneToMany Annotation Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report @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 AnnotationExample 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. Comment More infoAdvertise with us Next Article Hibernate - @OneToMany Annotation C chaitanyamunje Follow Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Hibernate- Annotations Practice Tags : Java Similar Reads 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 4 min read Hibernate - @ManyToMany Annotation @ManyToMany annotation in Hibernate is used to obtain many-to-many relationships between two entities. It allows us to create a bidirectional relationship between two entities where each entity can be associated with another entity through multiple instances. Examples of @ManyToMany Annotation Examp 4 min read Hibernate - @Inheritance Annotation The @Inheritance annotation in JPA is used to specify the inheritance relation between two entities. It is used to define how the data of the entities in the hierarchy should be stored in the database. The @Inheritance annotation provides us with benefits to reduce code complexity by creating a bas 6 min read Hibernate - @ManyToOne Annotation @ManytoOne annotation in Hibernate is used to create a many-to-one relationship between two entities. The @ManyToOne annotation indicates that the many instances of one entity can be associated with only one instance of another entity. When we annotate a field of the method with @ManyToOne annotatio 4 min read Hibernate - @MapsId Annotation @MapsId annotation in Hibernate is used to obtain a one-to-one relationship between two entities by mapping the primary key of one entity to the foreign key of another entity. This annotation is used when we have to use a shared primary key between two entities. Examples for @MapsId AnnotationExam 3 min read Hibernate - @Lob Annotation Many times there is a scenario while storing the data in the database that we have to store images or files within our database for a specific entity. In that case, we can use @Lob annotation which will help us to map large binary objects or large character objects to a specific entity in our databa 3 min read Hibernate - Annotations Annotation in JAVA is used to represent supplemental information. As you have seen @override, @inherited, etc are an example of annotations in general Java language. For deep dive please refer to Annotations in Java. In this article, we will discuss annotations referred to hibernate. So, the motive 7 min read Hibernate - @PrimaryKeyJoinColumn Annotation @PrimaryKeyJoinColumn in Hibernate is used to specify the primary key of the associated entity as the foreign key of the current entity. This annotation is used to establish a one-to-one relationship between the two entities, where the primary key of one entity is considered the foreign key in anoth 4 min read When to Use @JoinColumn Annotation in Hibernate? The @JoinColumn annotation in Hibernate is used to specify the mapping of a foreign key column in a relationship between two entities. The @JoinColumn annotation is applied on the owning side of the association to define the foreign key column name and other attributes which are related to the join 3 min read Hibernate - @OrderBy Annotation with Example @OrderBy annotation is used in Hibernate to specify the ordering of the elements in the collection valued property of an entity class. It is used to define the order of the elements which should be displayed. We can order the data within the entity using a specific parameter in ascending or descendi 4 min read Like