When you manage instances of hibernated objects, they must be βattachedβ to the session.
If you create an object with new , you first need to connect it to the session before you can manage it using sleep mode.
When an object (the generated identifier) ββis id, hibernate expects this object to exist in its session (since the id value can exist only if sleep mode generated it or sleep mode brought it from the database through a request), otherwise case it throws a Stale exception.
You need to either call saveOrUpdate on it for hibernate to create your identifier, and attach an instance to the session (in case it does not exist in the database), or call load with the id for hibernation to bring an instance from the database (if it exists in the database).
In this case, you know the identifier, so you need to request sleep mode to get the attached instance. So try this instead:
SystemUsersModel obj = session.load(SystemUsersModel.class, userlist.getId()); session.delete(obj);
Here is an explanation of the different states of the sleeping session instance: https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html
Edit : thanks @vanoekel
Or better, you can simply use getReference instead of load , as it will be less expensive in terms of resources if you just want to remove its afterword.
gmanjon
source share