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

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

@AttributeOverrides and @AttributeOverride Annotations

@AttributeOverrides and @AttributeOverride Annotations
In our previous example we had seen how to inject one entity into another entity(here). We have injected the address class into employee class. In that example we have not renamed the fields in the address class (city, state and pincode). We accepted the default field names – city, state and pincode.

In this example we are going to see, how we are going to change the name of the fields for the members in address class. There are 2 ways of doing it,

Method 1:
This method is similar to previous example. Just like we used @Column annotation in employee class, we can use the same annotation in address class and rename the database column name.

Method 2:
We can change the name of the column for the members in address class using @AttributeOverrides and @AttributeOverride annotations.

Method 1 has already been explained in previous example, Lets see the example for Method 2 in this article.

Structure
structure

hibernate.cfg.xml

NewHibernateUtil.java

address.java

employee.java

In employee.java, We have used @AttributeOverrides – This annotation is used when we have to change the column names for more than one member and @AttributeOverride is the annotation used to change the column name for each variable

Execution.java

Output
op1

We can see that the column names has been changed to City_Override, Pincode_Override and Street_Override

 

 

 

 

Read More

@Embeddable and @Embedded Annotation – Hibernate

@Embeddable and @Embedded Annotation – Hibernate
We have seen some basic annotations of hibernate (here), Now we are going to see about @Embeddable and @Embedded annotations.

One entity can be embedded into another entity, One entity attributes may be common to more than one entity.

Let us understand with an example,
Consider we have 2 pojo’s – employee and manager. We create another pojo named address. Here address attribute is common for both employee and manager. Address has common attributes that are required by both these java classes. @Embeddable and @Embedded annotations play a major role in embedding address into employee and manager entities.

Lets see @Embeddable and @Embedded example for employee entity.

Structure
structure

address.java

employee.java

NewHibernateUtil.java

hibernate.cfg.xml

Output
op1

op2

Note:
If you like to change the column name of address.java, use the same annotations of employee.java – @Column (name=””)

 

 

Read More