Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate is *NOT* throwing an exception (I think it should)

Tags:

java

hibernate

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.

like image 568
André Chalella Avatar asked Jun 03 '09 20:06

André Chalella


People also ask

What is hibernate exception?

Hibernate Exception Overview Many conditions can cause exceptions to be thrown while using Hibernate. These can be mapping errors, infrastructure problems, SQL errors, data integrity violations, session problems, and transaction errors. These exceptions mostly extend from HibernateException.

What happens if an exception is thrown?

When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.

How to handle Hibernate exception?

Hibernate works with most of unchecked Hibernate persistence layer exceptions. When Hibernate interacts with the database it throws SQLException . Hibernate provides better handle than the JDBCException . Developers can use the try and catch block to handle the exceptions.

When to use Hibernate load method?

Only use load() method if you are sure that the object exists. If you are not sure that the object exist, then use one of get() methods. load() method will throw an exception if the unique id is not found in the database. get() method will return null if the unique id is not found in the database.


2 Answers

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.

like image 87
Artem Tikhomirov Avatar answered Sep 28 '22 09:09

Artem Tikhomirov


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

like image 34
DarkSquid Avatar answered Sep 28 '22 09:09

DarkSquid