These are chat archives for hibernate/hibernate-orm

29th
Jul 2017
Declan Curran
@dcrn
Jul 29 2017 04:37

Hi guys, I'm having a small problem with Hibernate (5.2.10.Final) here. My code is fairly simple, just updating a object, then finding that same object again (Within the same transaction). This is usually fine, but my problem comes in when I detach the found object:

entityManager.getTransaction().begin();
UserModel updateUser = entityManager.find(UserModel.class, id);
updateUser.setUsername("Bob");

UserModel foundUser = entityManager.find(UserModel.class, id);
entityManager.detach(foundUser);
entityManager.getTransaction().commit();

In this situation, because the foundUser object is detached, the object will never get updated.
The behaviour here is really strange to me, because the foundUser object has the new updated values.
I checked entityManager.isDirty, and it is correct (Dirty), up until I detach the foundUser object, at which point it becomes clean...

Just on a hunch I tried to merge and detach the foundUser object again:

entityManager.getTransaction().begin();
UserModel updateUser = entityManager.find(UserModel.class, id);
updateUser.setUsername("Bob");

UserModel foundUser = entityManager.find(UserModel.class, id);
entityManager.detach(foundUser);
entityManager.merge(foundUser);
entityManager.detach(foundUser);
entityManager.getTransaction().commit();

and this worked!

Am I missing something here? Is this a bug or is there some reason behind this behaviour?

ryan2049
@ryan2049
Jul 29 2017 05:04
I'm having trouble understanding what you're trying to accomplish and what is failing
entityManager.find() is going to return the object from the session, so it would make sense that the updates are existing in it
Declan Curran
@dcrn
Jul 29 2017 05:13
After updating the model, I found the same model again and detached it. No updates were persisted, despite the found model having the updated data.
ryan2049
@ryan2049
Jul 29 2017 05:15
I'd have to look ,
Sorry my phone bugged
Is there a reason you don't call merge() after making some changes?
Declan Curran
@dcrn
Jul 29 2017 05:18
Even with merge the same issue exists.
entityManager.getTransaction().begin();
UserModel updateUser = entityManager.find(UserModel.class, id);
updateUser.setUsername("Bob");
entityManager.merge(updateUser);

UserModel foundUser = entityManager.find(UserModel.class, id);
entityManager.detach(foundUser);
entityManager.getTransaction().commit();
Merging the updateUser after I detach foundUser works, but that doesn't really work in the flow of my application. And using detach -> merge -> detach on foundUser is still working.
Declan Curran
@dcrn
Jul 29 2017 06:25
I was able to get around the issue by replacing find with a criteria query, but this isn't really ideal for me. This seems like a bug in the session cache.
Derek Hardison
@DerekHardison_twitter
Jul 29 2017 12:56
I'm using inheritance and have a subclass that doesn't actually have any special attributes in it (at least for now...) -- is there a way I can not have to create a special table for that subclass with a single column (i.e. id fk).... I need it for some other subclasses just not this one.
ryan2049
@ryan2049
Jul 29 2017 13:38
@dcrn I still don't understand why you're detaching the object?