CRUD Operations in Hibernate

CRUD Operations in Hibernate
So far we have seen Joined class, table per hierarchy and Mappings (OneToOne, OneToMany, ManyToMany)

Now let us see the CRUD Operations (Create, Read, Update and Delete)

Structure:
structure

We are going to add 2 employee’s, Retrieve one employee, Update and delete one employee record

hibernate.cfg.xml

NewHibernateUtil.java

employee.java

execution.java

Output:
Inserting Values 

op1

op2

Reading Values
op3

Update Values
op4

op5

Delete Values
op6

op7

Note:
Run each operations separately (comment other operations) – Don’t run all the operations simultaneously.

 

Read More

Joined Strategy in Hibernate using Annotations

Joined Strategy in Hibernate using Annotations

In our previous example we have seen Single Table Strategy using Annotations. Here lets see the example of Joined Table in hibernate using Annotations

First let us understand the concept of Joined Strategy.

Joined Strategy

h1

 

Here we can see that for each class, Employee, Landline and Mobile separate tables are created and the data’s respective of those classes are saved. But the reference ID number is inserted in employee table. This is known as Joined Strategy

Lets see an example for this,

structure

employee.java

landline.java

mobile.java

NewHibernateUtil.java

hibernate.cfg.xml

execution.java

Output:
op1

Annotate_Employee
op2

Here we can see the names and reference ID’s are inserted into Annotate_Employee table.

mobile
op3

landline
op4

 

 

 

Read More

Table per Hierarchy using Annotations (Single Table)

Table per Hierarchy using Annotations (Single Table)
We have already seen an example for Table per hierarchy (here). In this article we are going to see how Table per Hierarchy can be implemented in Hibernate using Annotations.

pic1

In this diagram, Employee is a class and Mobile and LandLine classes extends employee class. When we insert the values in them and when we run, all the data will be accumulated in a Single table. This is known as Table per Hierarchy.

Lets see this example in Detail,

Structure
structure

employee.java

mobile.java

landline.java

NewHibernateUtil.java

hibernate.cfg.xml

execution.java

 

Output:
op1

From the output we can see that, we have used @Entity annotation in both mobile and landline classes but hibernate has created only one table Annotate_Employee and has inserted both the values of mobile and landline in single table

op2

There is one column – DTYPE that differentiates the classes and the data

 

 

 

 

Read More

ManyToMany Mapping in Hibernate using Annotations

ManyToMany Mapping in Hibernate using Annotations
In our previous examples we have seen OneToOne and OneToMany mappings. In this article let us see how to implement ManyToMany annotation in Hibernate using Annotations.

Let us understand how ManyToMany mapping works. For example, let us consider 2 entities – Employee and Address. An employee can have more than one address let us assume permanent and temporary address, Same way more than one employee would have stayed in the temporary address. This scenario shows Employee can have more than one address and an address can have more than one employee. This is ManyToMany mapping.

screen1

This is a simple example, From the diagram we can understand that an employee 1 has more than one address associated with him, same way temporary address has more than one employee associated with it. This is known as ManyToMany mapping.

Now let us see an example.

Structure
Structure

hibernate.cfg.xml

NewHibernateUtil.java

Address.java

Employee.java

Execution.java

Output:
op1

From the output,
Employee_Mapping table saves both the employee records

op4

Address_Mapping table saves both the addresses

op5

Employee_Mapping_Address_Mapping has the employee ID mapped to the corresponding address
op2
we can see that employee with id: 1 is mapped with address ID 1 and 2

 Address_Mapping_Employee_Mapping
Same way here the address 1 is mapped to both Employee’s with ID 1 and 2
op3

 

 

 

Read More

OneToMany in Hibernate using Annotations

OneToMany in Hibernate using Annotations
Here we are going to implement OneToMany mapping in hibernate using Annotations.

We are going to have 2 entities Address and Employee. Here we are assuming that an employee can have more than one address. When we enter the employee details and his addresses (Here we are going to enter 2 addresses for one employee) a 3rd table creating which brings the relationship between the both Employee and Address table.

op1

Once we go through the example, the concept will be clear.

Structure:
Structure

hibernate.cfg.xml

NewHibernateUtil.java

Address.java

Employee.java

Execution.java

Output:
op1

We can see 3 tables has been created, Employee_Mapping, Address_Mapping and Joined_Table. Here the relationship between the Employee_Mapping and Address_Mapping is pointed out in JoinedTable

op4

we can see that employee_ID – 3, that employee has 2 address that are mapped in address table (5,6)

This is know an OneToMany Mapping

 

 

 

 

Read More