Hibernate - @ManyToOne Annotation Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report @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 annotation the Hibernate will create the many-to-one relation between these two entities. This annotation is used to map a single-valued annotation where multiple instances of one entity are associated with a single instance of another entity. Examples of @ManyToOne 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 // which is the generated value. @Id @GeneratedValue private long id; // on the below line creating a field for section name. private String sectionName; // on the below line creating a field for students name // array list present in that section. private ArrayList<String> studentsNameList; // on the below line creating a field for school which // we are annotating with many to one annotation. @ManyToOne private School school; } // on the below line creating an entity for School. @Entity public class School { // on the below line creating an id for school. @Id @GeneratedValue private long id; // on the below line creating a field for the school // name. private String schoolName; } Code Explanation: In the above example we are creating two entities for School and Section and these entities are linked with each other through many-to-one annotation. We are creating different fields in section entities such as id for the section which is annotated with @Id and @GeneratedValue to generate it automatically. Then we are creating a field for section name, students name array list which is an array of student’s names present in that section. Lastly, we are creating a file for the section which is annotated with @ManyToOne. @ManyToOne annotation indicates that the section entity is having a many-to-one relationship with the School entity. The @ManyToOne annotation is used to annotate the school field in the Section entity which indicates that the section belongs to a single school. Example 2: Java // 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; } // 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; } Code Explanation: In the above example, we are creating two entities, one for the Employee and another for the Department. The employee entity consists of different fields such as employee name, age, gender, and id which are annotated with @Id and @GeneratedValue to generate it automatically. Then we are creating one more field for the department which we are annotating with @ManyToOne which indicates the current employee is associated In many to one relationship with the Department entity. Then we are creating an entity for the Department which has fields such as id which is auto-generated as we are adding @GeneratedValue annotation to it. Then we are creating a field for the name of the department. Comment More infoAdvertise with us Next Article Hibernate - @Inheritance Annotation C chaitanyamunje Follow Improve Article Tags : Java Java-Hibernate Java-Spring-Data-JPA Hibernate- Annotations Practice Tags : Java Similar Reads 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 - @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 - @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 AnnotationExample 1: Java // on the be 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 - @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 - @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 Like