use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DefaultInitializeCollectionEventListener method onInitializeCollection.
/**
* called by a collection that wants to initialize itself
*/
public void onInitializeCollection(InitializeCollectionEvent event) throws HibernateException {
PersistentCollection collection = event.getCollection();
SessionImplementor source = event.getSession();
CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
if (ce == null) {
throw new HibernateException("collection was evicted");
}
if (!collection.wasInitialized()) {
final boolean traceEnabled = LOG.isTraceEnabled();
if (traceEnabled) {
LOG.tracev("Initializing collection {0}", MessageHelper.collectionInfoString(ce.getLoadedPersister(), collection, ce.getLoadedKey(), source));
LOG.trace("Checking second-level cache");
}
final boolean foundInCache = initializeCollectionFromCache(ce.getLoadedKey(), ce.getLoadedPersister(), collection, source);
if (foundInCache) {
if (traceEnabled) {
LOG.trace("Collection initialized from cache");
}
} else {
if (traceEnabled) {
LOG.trace("Collection not cached");
}
ce.getLoadedPersister().initialize(ce.getLoadedKey(), source);
if (traceEnabled) {
LOG.trace("Collection initialized");
}
if (source.getFactory().getStatistics().isStatisticsEnabled()) {
source.getFactory().getStatisticsImplementor().fetchCollection(ce.getLoadedPersister().getRole());
}
}
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DefaultLockEventListener method onLock.
/**
* Handle the given lock event.
*
* @param event The lock event to be handled.
* @throws HibernateException
*/
public void onLock(LockEvent event) throws HibernateException {
if (event.getObject() == null) {
throw new NullPointerException("attempted to lock null");
}
if (event.getLockMode() == LockMode.WRITE) {
throw new HibernateException("Invalid lock mode for lock()");
}
if (event.getLockMode() == LockMode.UPGRADE_SKIPLOCKED) {
LOG.explicitSkipLockedLockCombo();
}
SessionImplementor source = event.getSession();
Object entity = source.getPersistenceContext().unproxyAndReassociate(event.getObject());
//TODO: if object was an uninitialized proxy, this is inefficient,
// resulting in two SQL selects
EntityEntry entry = source.getPersistenceContext().getEntry(entity);
if (entry == null) {
final EntityPersister persister = source.getEntityPersister(event.getEntityName(), entity);
final Serializable id = persister.getIdentifier(entity, source);
if (!ForeignKeys.isNotTransient(event.getEntityName(), entity, Boolean.FALSE, source)) {
throw new TransientObjectException("cannot lock an unsaved transient instance: " + persister.getEntityName());
}
entry = reassociate(event, entity, id, persister);
cascadeOnLock(event, persister, entity);
}
upgradeLock(entity, entry, event.getLockOptions(), event.getSession());
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class WrapVisitor method processCollection.
@Override
Object processCollection(Object collection, CollectionType collectionType) throws HibernateException {
if (collection != null && (collection instanceof PersistentCollection)) {
final SessionImplementor session = getSession();
PersistentCollection coll = (PersistentCollection) collection;
if (coll.setCurrentSession(session)) {
reattachCollection(coll, collectionType);
}
return null;
} else {
return processArrayOrNewCollection(collection, collectionType);
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class WrapVisitor method processArrayOrNewCollection.
final Object processArrayOrNewCollection(Object collection, CollectionType collectionType) throws HibernateException {
final SessionImplementor session = getSession();
if (collection == null) {
//do nothing
return null;
} else {
CollectionPersister persister = session.getFactory().getCollectionPersister(collectionType.getRole());
final PersistenceContext persistenceContext = session.getPersistenceContext();
//TODO: move into collection type, so we can use polymorphism!
if (collectionType.hasHolder()) {
if (collection == CollectionType.UNFETCHED_COLLECTION) {
return null;
}
PersistentCollection ah = persistenceContext.getCollectionHolder(collection);
if (ah == null) {
ah = collectionType.wrap(session, collection);
persistenceContext.addNewCollection(persister, ah);
persistenceContext.addCollectionHolder(ah);
}
return null;
} else {
PersistentCollection persistentCollection = collectionType.wrap(session, collection);
persistenceContext.addNewCollection(persister, persistentCollection);
if (LOG.isTraceEnabled()) {
LOG.tracev("Wrapped collection in role: {0}", collectionType.getRole());
}
//Force a substitution!
return persistentCollection;
}
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DefaultSaveOrUpdateEventListener method onSaveOrUpdate.
/**
* Handle the given update event.
*
* @param event The update event to be handled.
*/
public void onSaveOrUpdate(SaveOrUpdateEvent event) {
final SessionImplementor source = event.getSession();
final Object object = event.getObject();
final Serializable requestedId = event.getRequestedId();
if (requestedId != null) {
//reassociating the proxy
if (object instanceof HibernateProxy) {
((HibernateProxy) object).getHibernateLazyInitializer().setIdentifier(requestedId);
}
}
// For an uninitialized proxy, noop, don't even need to return an id, since it is never a save()
if (reassociateIfUninitializedProxy(object, source)) {
LOG.trace("Reassociated uninitialized proxy");
} else {
//initialize properties of the event:
final Object entity = source.getPersistenceContext().unproxyAndReassociate(object);
event.setEntity(entity);
event.setEntry(source.getPersistenceContext().getEntry(entity));
//return the id in the event object
event.setResultId(performSaveOrUpdate(event));
}
}
Aggregations