@SuppressWarnings("unchecked") @Override /** * Handle the given delete event. This is the cascaded form. * * @param event The delete event. * @param transientEntities The cache of entities already deleted * @throws HibernateException */ public void onDelete(DeleteEvent event, Set transientEntities) throws HibernateException { final EventSource source = event.getSession(); final PersistenceContext persistenceContext = source.getPersistenceContext(); Object entity = persistenceContext.unproxyAndReassociate(event.getObject()); EntityEntry entityEntry = persistenceContext.getEntry(entity); final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity); final Object version; if (persister.isVersioned()) { version = persister.getVersion(entity, source.getEntityMode()); // Make sure version has not changed on deleted entities if ((entity instanceof TimelineEntity) && !((TimelineEntity) entity).isNew()) { if (!persister.getVersionType().isEqual(version, entityEntry.getVersion())) { throw new StaleObjectStateException(persister.getEntityName(), entityEntry.getId()); } } } super.onDelete(event, transientEntities); }
/** * Given that there is no pre-existing proxy. Check if the entity is already loaded. If it is, * return the entity, otherwise create and return a proxy. */ private Object createProxyIfNecessary( final LoadEvent event, final EntityPersister persister, final EntityKey keyToLoad, final LoadEventListener.LoadType options, final PersistenceContext persistenceContext) { Object existing = persistenceContext.getEntity(keyToLoad); if (existing != null) { // return existing object or initialized proxy (unless deleted) log.trace("entity found in session cache"); if (options.isCheckDeleted()) { EntityEntry entry = persistenceContext.getEntry(existing); throwObjectDeletedIfNecessary(event, entry); } return existing; } else { log.trace("creating new proxy for entity"); // return new uninitialized proxy Object proxy = persister.createProxy(event.getEntityId(), event.getSession()); persistenceContext.getBatchFetchQueue().addBatchLoadableEntityKey(keyToLoad); persistenceContext.addProxy(keyToLoad, proxy); return proxy; } }