Category Archives: Hibernate

How to configure Composite id in Hibernate ?

In a database, we use a primary key, to identify one row uniquely among multiple rows stored in a table. In most of the cases, a single column of table is suitable to use it as a primary key, but in some cases, we need combination of two or more columns is needed to uniquely identify a row. It is called a Composite Primary Key.
In database, a table can have maximum a single primary key. In hibernate, to map the properties of POJO class to a composite primary key, we use composite-id tag.
Continue reading How to configure Composite id in Hibernate ?

Component Mapping In Hibernate ( HAS-A Relationship) .

Component Mapping is used, when there is a HAS-A relationship between the POJO classes. In component mapping, object of a class will be stored as a value of another class object. In a HAS-A relationship, we call the first class as a dependent and second class as dependency. If we want to save a dependent object, along with its dependency object in a single table of database then we need to use component mapping of hibernate.
Continue reading Component Mapping In Hibernate ( HAS-A Relationship) .

Inheritance Mapping / Inheritance Strategies in Hibernate.

In a Hibernate application if there are multiple POJO classes and if they have common properties then to get reusability we apply inheritance.
Common properties, we separate and we create in super class and we extends super class to multiple sub-classes.
For example, we have two POJO classes in a project called CreditCard and Cheque like the following :-
Continue reading Inheritance Mapping / Inheritance Strategies in Hibernate.

DAO Design Pattern.

A design pattern is not a Class/Interface/API or technology or a framework.
DAO is a design pattern, a design pattern is a best solution for a recursive problems.
In a real time project, if we mix business logic and a persistent logic then we face following problems:-
Continue reading DAO Design Pattern.

Why we need to make SessionFactory object of Hibernate as Singleton ?

In Hibernate, a SessionFactory object is the only one heavy weight object, because it stores Configuration data and all the Mapping data of the project.
If our project is a Desktop Application then there is no need to make SessionFactory object as a Singleton, because only one user can access the Application at a time.
Continue reading Why we need to make SessionFactory object of Hibernate as Singleton ?

Difference Between update() and merge() method in Hibernate ?

Both methods are used for converting the state of an instance from Detached State to Persistent State.
When we call update() method and if already an object with same POJO class and same Id already existing in the Cache then update() method throws NonUniqueObectException , because a Session Cache of an Hibernate can not maintain two object of same POJO class with same Id.
Continue reading Difference Between update() and merge() method in Hibernate ?

Instance States in Hibernate.

In hibernate, there are three states defined for a POJO class object in Hibernate:-
1.) Transient State.
2.) Persistent State.
3.) Detached State.

Continue reading Instance States in Hibernate.

Custom Generator Class In Hibernate.

Every generator class whether it is a pre-defined generator class or an user-defined generator class, must implement org.hibernate.id.IdentifierGenerator Interface.
Identifier Generator Interface has only one abstract method called generate( – , – ).
We need to override this generate() method and we need to defined the generator logic in this method.
Continue reading Custom Generator Class In Hibernate.

Generator Classes in Hibernate.

A Generator class generates id for an object. Hibernate calls a generator class, before going insert an object into a database.
A generator class returns an id and hibernate will set that id to an object and then inserts that object to database. There are multiple pre-defined generator class are exist in hibernate , if existing generators are not enough then we can also create a Custom generator class. In .hbm file , under tag , we need to write tag as a sub-tab, to tell the hibernate about which generator class is should call before going to insert an object to database. For all the pre-defined generator classes, there are alias name.
Continue reading Generator Classes in Hibernate.

Difference Between save() and persist() method in Hibernate ?

persist() method will save an object in the Database, but it does not return Id of the saved object. save() method will save the object to the Database and also returns Id of the saved object , in the form of Serializable type. persist() method return type is void and save() method return type is Serializable.