Hibernate mapping annotations

In this section, you will learn about the annotations used for various mapping in Hibernate.


 

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

Hibernate mapping annotations

In this section, you will learn about the annotations used for various mapping in Hibernate.

Given below mapping annotations under different type of mapping :

One-to-One Mapping

The worker and workerdetail have one-to-one mapping which can be symbolically shown as below :

worker----------------->workerdetail

In Worker class, do the following mapping :


@OneToOne(mappedBy = "worker", cascade = CascadeType.ALL)
private WorkerDetail workerDetail;

In WorkerDetail class, do the following mapping :


@OneToOne
@PrimaryKeyJoinColumn
private Worker worker;

One-to-Many Mapping

The employee and address table have One-to-Many relation as follows :

The Address class should have following mapping annotation :


@OneToMany(mappedBy = "address")
private Set<Employee> employees;

The mapping annotation of Employee class are given below :


@OneToMany(mappedBy = "address")
private Set<Employee> employees;

Many to Many Mapping

Many student may have register for various courses  and a course may have many registered student. The Many to Many mapping between student and course table can be shown as follows:

The mapping annotation of Student class is given below:


@ManyToMany(cascade = {CascadeType.ALL})
@JoinTable(name="student_course",[email protected](name="student_id")},[email protected](name="course_id")})
private Set<Course> courses = new HashSet<Course>();

The Course class mapping annotation is given below :


@ManyToMany(mappedBy="courses")
private Set<Student> students = new HashSet<Student>();

NOTE : student_course table doesn't have pojo class neither it have mapping annotation needed in the above cases.

 

Advertisements

Share on Google+Share on Google+

Hibernate mapping annotations

Posted on: April 17, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate mapping annotations  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads