Blog Archives

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 ...

Tags:

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 ...

Tags:

05: Debugging Hibernate & handling 4 common issues

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

In the log4j.properties 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 ...


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

Java & Big Data Tutorials

Top