Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Hibernate Transaction

Posted on 2007-07-19
Medium Priority
Last Modified: 2013-11-23
Simple Hibernate question.

If i was within a transaction, when will hibernate throw errors if there are database conflicts.

Is it only on commit? or does hibernate do any fancy checking when you do a .save() .update()?
Question by:dboyd6868
LVL 92

Accepted Solution

objects earned 172 total points
ID: 19527762
would think it would be only on commits
LVL 17

Assisted Solution

by:Dejan Pažin
Dejan Pažin earned 164 total points
ID: 19531256

Some errors will be thrown at method call time. You can see that from the specification of the methods. E.g. update method comment says:

       * Update the persistent instance with the identifier of the given detached
       * instance. If there is a persistent instance with the same identifier,
       * an exception is thrown. This operation cascades to associated instances
       * if the association is mapped with <tt>cascade="save-update"</tt>.
       * @param object a detached instance containing updated state
       * @throws HibernateException

I dont know if that's fancy though :)
LVL 19

Assisted Solution

by:Jim Cakalic
Jim Cakalic earned 164 total points
ID: 19532453
The Session will periodically execute the SQL statements needed to synchronize the JDBC connection's state with the state of objects held in memory. This process is called flushing. It occurs by default at several points: before some query executions, when Transaction.commit() is called, or when Session.flush() is called.

According to the Hibernate reference, except when you explicity flush(), there are absolutely no guarantees about when the Session executes the JDBC calls, only the order in which they are executed. However, Hibernate does guarantee that the Query.list(..) will never return stale data; nor will they return the wrong data.

It is possible to change the default behavior so that flush occurs less frequently using Session.flushMode(..). The FlushMode class defines three different modes: only flush at commit time (and only when the Hibernate Transaction API is used), flush automatically using the explained routine, or never flush unless flush() is called explicitly. The last mode is useful for long running units of work, where a Session is kept open and disconnected for a long time.

In writing Hibernate Repository (or DAO) classes, I've found it sometimes helpful to explicitly flush at certain points so any SQLExceptions (constraint or primary key violations for example) happen closer to the point in the code where persistent object state is changing.

For more details and code examples, please refer to

Jim Cakalic

Expert Comment

ID: 20213285
Forced accept.

EE Admin

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question