Blog Archives
1 2

02: Understanding Hibernate proxy objects and avoiding potential pitfalls

Q. How does hibernate support lazy loading? A. Hibernate uses a proxy object to support lazy loading. Basically as soon as you reference a child or lookup object via the accessor/getter methods, if the linked entity is not in the session cache (i.e. the first-level cache), then the proxy code…

Read more ...

03: Identifying and fixing NonUniqueObjectException in Hibernate

NonUniqueObjectException is thrown when there is an object already associated with the session with the same id (primary key) (i.e. a duplicate). It is important to understand the concept of a “detached” object in Hibernate. This is one of the most common errors, understanding why and when this error is thrown will save you time in identifying and fixing this issue.

Q. What is a detached object in Hibernate?

When you close an individual Hibernate Session, the persistent objects you are working with become detached. This means the objects are still in the application’s memory, but Hibernate is no longer responsible for tracking changes to those detached objects. If you then modify a detached object and want to update it, you have to reattach that object. During that reattachment process, Hibernate will verify if there are any other copies of the same object. If it finds any already in the session, it gets confused as to which one is the correct object, hence notifies you with a “NonUniqueObjectException” as opposed to saving any bad data.

LazyInitializationException” is another common error due to a detached object. This error occurs when you try to access properties or associated object of Detached object then it results in Lazy Initialization Exception. This means Hibernate “could not initialize the proxy” when there is no Session”.

Scenario 1: whilst update(..) or saveOrUpdate(..)

Step 1: Load an entity say a “Book” from the database into Hibernate session.… Read more ...


04: Identifying and fixing LazyInitializationException in Hibernate

LazyInitializationException is thrown when an object becomes detached, and if you try to access associated (i.e. proxied) object(s) of a detached object.

Q. What is a detached object in Hibernate?

When you close an individual Hibernate Session, the persistent objects you are working with are detached. This means the object is still in the application’s memory, but Hibernate is no longer responsible for tracking changes to that object. If your detached object has any associated objects, for example a “Book” object might have an associated proxied “Author” object. So, when you do something like

Hibernate needs to go database to initialize the proxy object. This is known as the “lazy” initialization of the “Author” object. Load it only when it is required. Now, if your Hibernate session is closed, then the database connection is closed and Hibernate cannot load the proxy object.

Q. What is a unit-of-work?

A session means a physical connection to a database. Hibernate session is a unit of work. You start a unit of work by “opening a session” and close the unit of work by closing a session. You also flush() a Session at the end of a unit of work to execute the SQL.… Read more ...


05: Debugging Hibernate & handling 4 common issues

#1. Enable Logging in your log4j.xml,, or myapp-logback.xml

In the file, you can enable Hibernate SQL logging as shown below

or in your log4j.xml or myapp-logback.xml as

You can then check your logs for the SQLs executed and the parameters passed. The example below shows 3 parameters passed to an update query

Read more ...

07: Hibernate mistakes – accessing objects outside of a transaction

Hibernate does require the same EntityManager to be available in order to lazily load objects. If you have no EntityManager, then you have no knowledge of the datastore. Once the transaction is committed the objects become detached, and you can’t lazy load detached objects. So, you need to lazily load…

Read more ...

08: Hibernate automatic dirty checking of persistent objects and handling detached objects

Q1. What do you understand by automatic dirty checking in Hibernate? A1. Dirty checking is a feature of hibernate that saves time and effort to update the database when states of objects are modified inside a transaction. All persistent objects are monitored by hibernate.It detects which objects have been modified…

Read more ...

09: Hibernate entities with auditable, soft delete & optimistic locking fields

There are certain generic columns that database tables have like Auto generated identity columns. Auditing columns like createdDtTm, createdBy, modifiedDtTm, and modifiedBy. Soft delete or logical delete flags like inactive ‘Y’ or ‘N’. Optimistic locking detection based on columns like Timestamp or version number. These logic can be mapped in…

Read more ...

10: Spring, JavaConfig (i.e @Configuration), JPA & Hibernate by example

Example #1 Wiring JDBC Datasource

application.yml where the datasource properties are read from.

Example #2 Wiring JDBC Datasource with JNDI

Read more ...

11: Hibernate dependency jars and plugins

Here is a sample maven pom.xml snippets containing the dependency jars. 

The above snippet defines the version numbers. The snippet below defines the dependencies.

Read more ...

12: Hibernate custom data type : Blob type example

There are times you want to define a custom data type in Hibernate. Here is an example of using a custom ImageType defined as a Blob databse type to store images, excel files, etc.

Step 1: Define the custom Hibernate data type “ImageTpe” as shown below.

Read more ...

1 2

500+ Enterprise & Core Java programmer & architect Q&As

Java & Big Data Tutorials