In this Hibernate CRUD Example, we will learn how to use Hibernate to perform CRUD operations using XML Mapping. We will be using Oracle database and build an Employee Management System which has the capabilities of Creating a new employee, Getting all the employees, Update the existing employee, Delete an employee. [Read more…]
What is the Difference between session.get() and session.load() in Hibernate ? This is one of the most asked interview question in Hibernate. Both the get() and load() methods are defined in the Session interface and used to retrieve the information of a specific record. In this article, we will see what is the difference between then and when to use which method. [Read more…]
Previously we have learned about Component Mapping in Hibernate using Annotations. In this example, we will learn about Hibernate Component Mapping using XML. In our example, we have two classes Employee and EmployeeAddress. The Employee can have an Address but Address cannot exist separately without Employee. Since the Employee and Address entities are strongly related, it is better to store them in a single table using component mapping(<component> tag). [Read more…]
In this example, we will learn about Component Mapping in Hibernate Using Annotations. Component Mapping represents the has-a relationship, the composition is stronger association where the contained object has no existence of its own. For example, Employee has an Address, an address cannot exist separately without Employee. Since the Employee and Address entities are strongly related, it is better to store them in a single table. [Read more…]
In our previous example, we have seen how to create a Composite key in Hibernate using <composite-id> tag and Annotations. In this Embeddable Composite Primary Key example, we will be declaring the IDs (Primary Key fields) as a separate class annotated with @Embeddable annotation. An Employee is identified by its EmployeeId, which is defined by empId and department. Let’s dig into the code…
If a database table has more than one column as the primary key then we call it as composite primary key. In this Hibernate Composite Primary Key tutorial, we will learn how to define Composite Primary Key using <composite-id> tag and Annotations. [Read more…]
Table per Class Hierarchy strategy will be having a single table for both the Parent class and the Sub class separated by a discriminator column. In Table per Subclass Hierarchy, subclass table will be mapped to the Parent class table by primary key and foreign key relationship. In Table Per Concrete Class Hierarchy also individual table will be created for each class. The disadvantage of this approach is that all the columns of the Primary class table will be again replicated in the Subclass table.
In the previous example we have seen Table per Class Hierarchy strategy, there we will be having a single table for both the Parent class and the Sub class separated by a discriminator column. In this Table per Subclass Hierarchy Example, subclass table will be mapped to the Parent class table by primary key and foreign key relationship.
Java being an Object Oriented Language, it supports one of the most powerful concept “Inheritance“. Object Oriented Systems generally supports both IS-A and HAS-A relationships, whereas Relational Model supports only HAS-A relationship. Hibernate can help you map such kind of entities into the Relational table based on the strategy selected.
Types of Inheritance in Hibernate
Hibernate offers 3 different approaches for mapping this kind of hierarchical classes.
- Table per Class hierarchy
- Table per Sub-Class hierarchy
- Table per Concrete Class hierarchy
In this Hibernate Many To Many Mapping Example, we will learn how Hibernate Many To Many relationship works(Using Annotation). Let’s take the Example of Employee and Department, one Employee can be part of many Departments and similarly one Department can have many Employees. Let’s dig into the code.