1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
public Session getHibernateSession() {
final Session session = this.getSession(false);
return session;
}
public void evict(final Object _obj) {
this.getHibernateTemplate().evict(_obj);
}
public void lock(final Object obj) {
this.getHibernateTemplate().lock(obj, LockMode.NONE);
}
@SuppressWarnings("unchecked")
public Object lockAndEvict(final Object _obj) {
if (_obj == null) {
return null;
}
if (_obj instanceof Collection) {
final Collection collection = (Collection) _obj;
final List newCollection = new ArrayList();
final Iterator iter = collection.iterator();
while (iter.hasNext()) {
newCollection.add(this.lockAndEvict(iter.next()));
iter.remove();
}
collection.addAll(newCollection);
return collection;
}
return this.lockAndEvictInternal(_obj);
}
protected Object lockAndEvictInternal(final Object _obj) {
try {
final Object obj2 = this.getObjectInHibernateSession(_obj);
if ((obj2 != null) && (obj2 != _obj)) {
this.debug("evicting " + obj2 + " for " + _obj);
this.evict(obj2);
}
this.lock(_obj);
} catch (final Exception excp) {
this.debug("cannot lock " + _obj);
}
return _obj;
}
public Object getObjectInHibernateSession(final Object _obj) {
Object result = null;
try {
final SessionImpl session = (SessionImpl) this.getHibernateSession();
Serializable identifier = null;
try {
identifier = session.getIdentifier(_obj);
} catch (final TransientObjectException excp) {
try {
identifier = session.getEntityPersister(null, _obj).getIdentifier(_obj,
session.getEntityMode());
} catch (final MappingException excp1) {
identifier = null;
}
}
if (identifier != null) {
final EntityKey key = new EntityKey(identifier, session.getEntityPersister(null,
_obj), session.getEntityMode());
result = session.getPersistenceContext().getEntity(key);
}
} catch (final Exception excp) {
this.debug("cannot recuparate object " + _obj + " from hibernate session");
}
return result;
} |