No row with the given identifier exists error during refresh operation after an update operation

I have one A class which has a one to many relation with a list of another B objects. The summary of the process is that we are always deleting all of the rows of B before inserting new ones. The code is kind of similar of this:

@Entity
public class A {
private Set b = new HashSet(0);
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “a”)
protected Set getBList() {
return this.b;
}
}

Session session = HibernateTool.getCurrentSession();
Criteria crit = session.createCriteria(A.class)
.setCacheable(true);
A existingObject = (A) crit.uniqueResult();

final Session session = HibernateTool.getCurrentSession();
final SQLQuery query = session.createSQLQuery(“DELETE FROM B WHERE A_ID = ?”);
query.setLong(0, getId());
query.executeUpdate();
getBList.clear();
session.save(existingObject);

        B b = new B();
    getBList().add(b);

//performs similar to saveOrUpdate
session.update(existingObject);

//Throws exception
session.refresh(existingObject);

Sometimes I’m getting this exception: org.hibernate.UnresolvableObjectException: No row with the given identifier exists: [B#470]

I know that issue happens when the entity does not exist in the database at the moment of the refresh, so I’m guessing the update sometimes does not consider all the properties, or I’m not doing the correct clean up before adding new ones (i.e getBList().clear() is wrong).

I saw this is a similar case https://hibernate.atlassian.net/browse/HHH-2305

I’m currently using hibernate core 4.2.27.Final-redhat-1

Also, I’m using second level cache for the entity A, and B, but not for the OneToMany association.

I found this as a possible workaround:

session.evict(existingObject);
session.load(existingObject, existingObject.getId());

Your help will be appreciated.

Thanks