Submitted by sneha on Wed, 10/21/2015 - 22:38
When referencing entities from other entities, we are mapping two entities whereas when referencing a value type we are embedding (or containing) a value type within an entity.
When mapping two entities, we annotate both classes as @Entity, and the mapping type is specified using the annotations @OneToOne, @OneToMany, @ManyToOne or @ManyToMany, over the reference variable declaration of an entity in another entity.
We will consider two entities for mapping examples – a student class and a course class.
Submitted by sneha on Wed, 10/21/2015 - 22:18
In Many-To-Many mapping, we refer to a collection/list of the entity from another entity and vice versa; and used @ManyToOne annotation on declarations of both entity collections.
We will assume that each course can have many students (many to many), and each student can do many course(many to many).
We will create two entity classes, Course and Student and do a many to many mapping from course to student, and again many to many mapping from student to course.
Submitted by sneha on Wed, 10/21/2015 - 17:50
In One-To-Many mapping, we refer to a collection/list of the entity from another entity and mark the reference variable as @OneToMany. The opposite of this relation from the second entity to the first will be Many-To-One and is annotated as @ManyToOne.
We will assume that each course can have many students(one to many), but each student can do only one course(many to one).
We will create two entity classes, Course and Student and do a one to one mapping from course to student, and many to one from student to course.
Submitted by sneha on Wed, 10/21/2015 - 17:37
In One-To-One mapping, we refer to one entity from another and mark the reference variable as @OneToOne.
In this example, we will consider two entity classes – student class and course class; and do a one to one mapping from course to student.
We will assume that there is a one-to-one mapping between a Course and Student – each course can be taken by only one student.
Submitted by sneha on Wed, 10/21/2015 - 10:34
When you load an embeding class, it may load its embedded collections either lazily or eagerly.
Important points about eager and lazy fetch types in Hibernate
-
By default, when you load an embedding class (E.g. User or Company) using session.get,
Pages