'hibernate: attempt to lock a deleted instance

I'm trying to lock an entity while working on it, and I'm doing the following:
1: retrieveing the entity from the database using this method:

@Override
public Optional<T> findById(final Serializable id) {
    Session session = getSession();
    T e = session.get(persistentClass, id);
    return Optional.ofNullable(e);
}

2: locking the entity:

public T myMethod(final Serialzable id){
   //...
   T entity = getById(id);
   LockRequest lr = session.buildLockRequest(LockOptions.UPGRADE);
   lr.setTimeOut(LOCK_REQUEST_TIME_OUT);
   lr.lock(entity); // <-- problem is in this line
   //...
}

but I get this exception:

org.hibernate.ObjectDeletedException: attempted to lock a deleted instance: [com.x.y.Entity#1]

As you can see I'm just retrieving the entity and locking it, not sure why the entity is in the deleted state!!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source