Hibernate Transaction

Posted on 2007-07-19
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

    would think it would be only on commits
    LVL 17

    Assisted Solution

    by:Dejan Pažin

    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
    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
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Suggested Solutions

    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now