6

The Hibernate JavaDoc states that Session.update(Object o) would raise an exception if there's already a persistent instance of o, right?

If there is a persistent instance with the same identifier, an exception is thrown.

However, the following code doesn't throw anything when I run it. And I think it should!

Email email = new Email("andre", "girafa", "hi");

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

session.save(email);
session.update(email);
session.update(email);

tx.commit();

// didn't throw... Let's try again

tx = session.beginTransaction();

session.update(email);
session.update(email);

tx.commit();
session.close();

// still nothing! :(

As you can say, twice I try to do multiple update()s, but still Hibernate's taking easy on me.

Anybody has a hunch why?

EDIT: it seems that it would only throw if another equivalent object, for instance, email2 with the same ID as email. I guess the documentation was kinda sloppy there.

1
  • Yes! the docs never want to write it clear and it lead me to this question!
    – Sam YC
    May 20, 2017 at 2:58

4 Answers 4

8

Update in Hibernate is not UPDATE in SQL language. Hibernate handles SQL UPDATEs automatically through state cache in Session object.

But it's only for entities loaded in current session. This method, session.update(object) is meant for attaching object from another tier to current session to track and, possible, update at the end.

In your case it's just an NOOP. It'll sourly throw if:

Email email = new Email("andre", "girafa", "hi");

Session session = factory.openSession();
Transaction tx = session.beginTransaction();

int id = session.save(email);
Email anotherEmail = new Email("", "", "");
anotherEmail.id = id;

session.update(anotherEmail);    // will throw

You could read more about update method semantics on Hibernate reference.

0
6

No error because it's the same instance you're updating.

The error is thrown if a DIFFERENT persistent instance is present in the session and you try to update().

1
  • 1
    i think it is not DIFFERENT persistent instance, it is DIFFERENT detached instance.
    – Sam YC
    May 20, 2017 at 2:58
0

Can you try with a session.flush()? To see if that raises the exception (sometimes commit may not flush data depending on flush mode).

Although I would say the exception is only thrown if the object was updated outside the scope of the current session, let's say by a concurrent client.

0

I suggest you always stick to EntityManager.merge instead of Hibernate update method, update is quite confusing.

I found this article explain very clear which I always refer to.

As with persist and save, the update method is an “original” Hibernate method that was present long before the merge method was added. Its semantics differs in several key points:

  • it acts upon passed object (its return type is void); the update method transitions the passed object from detached to persistent
    state;
  • this method throws an exception if you pass it a transient entity.

In the following example we save the object, then evict (detach) it from the context, then change its name and call update. Notice that we don’t put the result of the update operation in a separate variable, because the update takes place on the person object itself. Basically we’re reattaching the existing entity instance to the persistence context — something the JPA specification does not allow us to do.

Person person = new Person();
person.setName("John");
session.save(person);
session.evict(person);

person.setName("Mary");
session.update(person);

Trying to call update on a transient instance will result in an exception. The following will not work:

Person person = new Person();
person.setName("John");
session.update(person); // PersistenceException!

In order to understand the part above, you need to understand the difference betweentransient and detached object.

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.