6

I'm wondering how to reduce SQL updates with my Hibernate (4) & Oracle setup.

A simple case might be this:

Open a session/transaction, create a new entity Xyz with session.save(), process some business logic, make some changes to Xyz and call session.update(), and let the session close as normal and Hibernate with commit to DB.

When committing, Hibernate will do an insert followed by an update -- but all it really needs to do is a single insert, but with the latest properties of Xyz, in this example.

Does anyone have any ideas/patterns for doing this sort of thing? Or is there any way to change Hibernate's behavior around this? Or what's your opinion on this -- is it a complete anti-pattern?

I know Hibernate is smart enough to omit multiple updates, when one update just overwrites another -- so why not similar for inserts?

This small snippet can reproduce the "issue":

MyEntity e = new MyEntity("xxx"); 
Session session = sessionFactory.openSession(); 
session.beginTransaction();
session.save(e); e.setName("yyy"); 
session.getTransaction().commit(); 
session.close();

By the way, I have no triggers to worry about.

This example is very simple and the performance problem might seem trivial, but in reality I am dealing with a more complex object (i.e., multiple inserts & updates), and high volumes, so avoiding updates would be great.

5
  • Couldn't you just move the save to the end of the transaction and apply the change to the entity before saving it to the DB? If not, let us know what prevents this approach so we can work on a solutions. Since I don't think there is a simple Solution built into Hibernate (not sure though).
    – Carsten
    Jun 5, 2013 at 14:03
  • That's a solution, BUT the problem is I'm using various other wired services that expect the object to be in the session (they read the object from session). And they are used in other places so re-factoring them is more work than it sounds like. Is there a way I could add the object to the session, and it would be marked as persistent, but not call save() (that is, the INSERT will not be generated) until the very end?
    – ConorD55
    Jun 5, 2013 at 14:39
  • How are you getting the enitity from the session in the other services?
    – Carsten
    Jun 5, 2013 at 14:55
  • 1
    I guess you will have to extend your example a bit more for me to grasp it. ^^" But if you are using different 'transactions' and something like 'session.get' there is no way around this since pretty much all of the Hibernate entity fetching methods aim to fetch persisted entities. If thats the case I fear there is no quick win here. The only chance could be to abuse the cache somehow (with built in methods or perhaps using reflection) and preventing flushing. But only thinking about this seems just wrong. xD
    – Carsten
    Jun 5, 2013 at 15:12
  • Hey, thanks for the input. The entity is re-read at various points based on an id handed to them -- which is created when the entity is instantiated! In reality, the entity has just been "saved" and will be in the cache, and not yet in the DB, since the transaction hasn't ended yet. It's all one big transaction. Think of an outer wrapper as the thing that creates the entity, and it calls other services for some business logic that might change properties of the entity. I agree that there is probably no quick win here too.
    – ConorD55
    Jun 5, 2013 at 17:01

1 Answer 1

1

Is there a way I could add the object to the session, and it would be marked as persistent, but not call save() (that is, the INSERT will not be generated) until the very end?

Consider re-factoring to use Session.persist(Object) instead of Session.save(Object) if you want to defer the execution of INSERT until the session is flushed and/or the transaction is closed.

From section 10.2 of the JBoss Hibernate Community documentation:

You can also use persist() instead of save(), with the semantics defined in the EJB3 early draft.

  • persist() makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long running conversations with an extended Session/persistence context.
  • save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is problematic in a long-running conversation with an extended Session/persistence context.

Another useful reference can be found in the top-left cell of the method-to-scenario grid at the top of the "Putting it All Together" section of this blog article that describes the behavior of persist() on objects that have never been persisted as follows:

  1. Object added to persistence context as new entity
  2. New entity inserted into database at flush()/commit()
1
  • 1
    I think that even though very informative, it may not answer the question completely. I found the same situation using a standard JPA entity manager and em.persist(e) and subsequently some update which eventually lead to an insert followed by an update in hibernate action queue. Jul 8, 2014 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.