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.
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.
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.
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.
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.
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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With