Java Code Examples for org.hibernate.engine.SessionImplementor#getEntityMode()

The following examples show how to use org.hibernate.engine.SessionImplementor#getEntityMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Loader.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static EntityKey getOptionalObjectKey(QueryParameters queryParameters, SessionImplementor session) {
	final Object optionalObject = queryParameters.getOptionalObject();
	final Serializable optionalId = queryParameters.getOptionalId();
	final String optionalEntityName = queryParameters.getOptionalEntityName();

	if ( optionalObject != null && optionalEntityName != null ) {
		return new EntityKey( 
				optionalId,
				session.getEntityPersister( optionalEntityName, optionalObject ), 
				session.getEntityMode()
			);
	}
	else {
		return null;
	}

}
 
Example 2
Source File: DefaultFlushEntityEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) {
	if ( persister.isSelectBeforeUpdateRequired() ) {
		Object[] snapshot = session.getPersistenceContext()
				.getDatabaseSnapshot(id, persister);
		if (snapshot==null) {
			//do we even really need this? the update will fail anyway....
			if ( session.getFactory().getStatistics().isStatisticsEnabled() ) {
				session.getFactory().getStatisticsImplementor()
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		else {
			return snapshot;
		}
	}
	else {
		//TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
		EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() );
		return session.getPersistenceContext()
				.getCachedDatabaseSnapshot( entityKey ); 
	}
}
 
Example 3
Source File: BasicLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Object getReplacement() {

		final SessionImplementor session = getSession();
		if ( isUninitialized() && session != null && session.isOpen()) {
			final EntityKey key = new EntityKey(
					getIdentifier(),
			        session.getFactory().getEntityPersister( getEntityName() ),
			        session.getEntityMode()
				);
			final Object entity = session.getPersistenceContext().getEntity(key);
			if (entity!=null) setImplementation( entity );
		}

		if ( isUninitialized() ) {
			if (replacement==null) {
				replacement = serializableProxy();
			}
			return replacement;
		}
		else {
			return getTarget();
		}

	}
 
Example 4
Source File: BagType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentElementHolder( session, (Element) collection );
	}
	else {
		return new PersistentBag( session, (Collection) collection );
	}
}
 
Example 5
Source File: SortedSetType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentElementHolder(session, persister, key);
	}
	else {
		PersistentSortedSet set = new PersistentSortedSet(session);
		set.setComparator(comparator);
		return set;
	}
}
 
Example 6
Source File: SortedMapType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentMapElementHolder(session, persister, key);
	}
	else {
		PersistentSortedMap map = new PersistentSortedMap(session);
		map.setComparator(comparator);
		return map;
	}
}
 
Example 7
Source File: MyListType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		throw new IllegalStateException("dom4j not supported");
	}
	else {
		return new PersistentMyList( session, (IMyList) collection );
	}
}
 
Example 8
Source File: AbstractOwnedSetType.java    From webdsl with Apache License 2.0 5 votes vote down vote up
@Override
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode() == org.hibernate.EntityMode.DOM4J ) {
		throw new IllegalStateException("dom4j not supported");
	}
	else {
		return new utils.PersistentOwnedSet( session, (java.util.Set) collection );
	}
}
 
Example 9
Source File: AbstractOwnedListType.java    From webdsl with Apache License 2.0 5 votes vote down vote up
@Override
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode() == org.hibernate.EntityMode.DOM4J ) {
		throw new IllegalStateException("dom4j not supported");
	}
	else {
		return new utils.PersistentOwnedList( session, (java.util.List) collection );
	}
}
 
Example 10
Source File: ListType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentListElementHolder(session, persister, key);
	}
	else {
		return new PersistentList(session);
	}
}
 
Example 11
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Delete an object
 */
public void delete(Serializable id, Object version, Object object, SessionImplementor session)
		throws HibernateException {
	final int span = getTableSpan();
	boolean isImpliedOptimisticLocking = !entityMetamodel.isVersioned() && entityMetamodel.getOptimisticLockMode() > Versioning.OPTIMISTIC_LOCK_VERSION;
	Object[] loadedState = null;
	if ( isImpliedOptimisticLocking ) {
		// need to treat this as if it where optimistic-lock="all" (dirty does *not* make sense);
		// first we need to locate the "loaded" state
		//
		// Note, it potentially could be a proxy, so perform the location the safe way...
		EntityKey key = new EntityKey( id, this, session.getEntityMode() );
		Object entity = session.getPersistenceContext().getEntity( key );
		if ( entity != null ) {
			EntityEntry entry = session.getPersistenceContext().getEntry( entity );
			loadedState = entry.getLoadedState();
		}
	}

	final String[] deleteStrings;
	if ( isImpliedOptimisticLocking && loadedState != null ) {
		// we need to utilize dynamic delete statements
		deleteStrings = generateSQLDeletStrings( loadedState );
	}
	else {
		// otherwise, utilize the static delete statements
		deleteStrings = getSQLDeleteStrings();
	}

	for ( int j = span - 1; j >= 0; j-- ) {
		delete( id, version, j, object, deleteStrings[j], session, loadedState );
	}

}
 
Example 12
Source File: AbstractEntityPersister.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Object initializeLazyProperty(String fieldName, Object entity, SessionImplementor session)
		throws HibernateException {

	final Serializable id = session.getContextEntityIdentifier( entity );

	final EntityEntry entry = session.getPersistenceContext().getEntry( entity );
	if ( entry == null ) {
		throw new HibernateException( "entity is not associated with the session: " + id );
	}

	if ( log.isTraceEnabled() ) {
		log.trace(
				"initializing lazy properties of: " +
				MessageHelper.infoString( this, id, getFactory() ) +
				", field access: " + fieldName
			);
	}

	if ( hasCache() ) {
		CacheKey cacheKey = new CacheKey(id, getIdentifierType(), getEntityName(), session.getEntityMode(), getFactory() );
		Object ce = getCache().get( cacheKey, session.getTimestamp() );
		if (ce!=null) {
			CacheEntry cacheEntry = (CacheEntry) getCacheEntryStructure().destructure(ce, factory);
			if ( !cacheEntry.areLazyPropertiesUnfetched() ) {
				//note early exit here:
				return initializeLazyPropertiesFromCache( fieldName, entity, session, entry, cacheEntry );
			}
		}
	}

	return initializeLazyPropertiesFromDatastore( fieldName, entity, session, id, entry );

}
 
Example 13
Source File: SetType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentElementHolder( session, (Element) collection );
	}
	else {
		return new PersistentSet( session, (java.util.Set) collection );
	}
}
 
Example 14
Source File: MapType.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public PersistentCollection wrap(SessionImplementor session, Object collection) {
	if ( session.getEntityMode()==EntityMode.DOM4J ) {
		return new PersistentMapElementHolder( session, (Element) collection );
	}
	else {
		return new PersistentMap( session, (java.util.Map) collection );
	}
}
 
Example 15
Source File: AbstractLockUpgradeEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Performs a pessimistic lock upgrade on a given entity, if needed.
 *
 * @param object The entity for which to upgrade the lock.
 * @param entry The entity's EntityEntry instance.
 * @param requestedLockMode The lock mode being requested for locking.
 * @param source The session which is the source of the event being processed.
 * @throws HibernateException
 */
protected void upgradeLock(Object object, EntityEntry entry, LockMode requestedLockMode, SessionImplementor source)
throws HibernateException {

	if ( requestedLockMode.greaterThan( entry.getLockMode() ) ) {
		// The user requested a "greater" (i.e. more restrictive) form of
		// pessimistic lock

		if ( entry.getStatus() != Status.MANAGED ) {
			throw new ObjectDeletedException(
					"attempted to lock a deleted instance",
					entry.getId(),
					entry.getPersister().getEntityName()
			);
		}

		final EntityPersister persister = entry.getPersister();

		if ( log.isTraceEnabled() )
			log.trace(
					"locking " +
					MessageHelper.infoString( persister, entry.getId(), source.getFactory() ) +
					" in mode: " +
					requestedLockMode
			);

		final CacheConcurrencyStrategy.SoftLock lock;
		final CacheKey ck;
		if ( persister.hasCache() ) {
			ck = new CacheKey( 
					entry.getId(), 
					persister.getIdentifierType(), 
					persister.getRootEntityName(), 
					source.getEntityMode(), 
					source.getFactory() 
			);
			lock = persister.getCache().lock( ck, entry.getVersion() );
		}
		else {
			ck = null;
			lock = null;
		}
		
		try {
			if ( persister.isVersioned() && requestedLockMode == LockMode.FORCE ) {
				// todo : should we check the current isolation mode explicitly?
				Object nextVersion = persister.forceVersionIncrement(
						entry.getId(), entry.getVersion(), source
				);
				entry.forceLocked( object, nextVersion );
			}
			else {
				persister.lock( entry.getId(), entry.getVersion(), object, requestedLockMode, source );
			}
			entry.setLockMode(requestedLockMode);
		}
		finally {
			// the database now holds a lock + the object is flushed from the cache,
			// so release the soft lock
			if ( persister.hasCache() ) {
				persister.getCache().release(ck, lock );
			}
		}

	}
}
 
Example 16
Source File: CollectionLoadContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Add the collection to the second-level cache
 *
 * @param lce The entry representing the collection to add
 * @param persister The persister
 */
private void addCollectionToCache(LoadingCollectionEntry lce, CollectionPersister persister) {
	final SessionImplementor session = getLoadContext().getPersistenceContext().getSession();
	final SessionFactoryImplementor factory = session.getFactory();

	if ( log.isDebugEnabled() ) {
		log.debug( "Caching collection: " + MessageHelper.collectionInfoString( persister, lce.getKey(), factory ) );
	}

	if ( !session.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( session ) ) {
		// some filters affecting the collection are enabled on the session, so do not do the put into the cache.
		log.debug( "Refusing to add to cache due to enabled filters" );
		// todo : add the notion of enabled filters to the CacheKey to differentiate filtered collections from non-filtered;
		//      but CacheKey is currently used for both collections and entities; would ideally need to define two seperate ones;
		//      currently this works in conjuction with the check on
		//      DefaultInitializeCollectionEventHandler.initializeCollectionFromCache() (which makes sure to not read from
		//      cache with enabled filters).
		return; // EARLY EXIT!!!!!
	}

	final Comparator versionComparator;
	final Object version;
	if ( persister.isVersioned() ) {
		versionComparator = persister.getOwnerEntityPersister().getVersionType().getComparator();
		final Object collectionOwner = getLoadContext().getPersistenceContext().getCollectionOwner( lce.getKey(), persister );
		version = getLoadContext().getPersistenceContext().getEntry( collectionOwner ).getVersion();
	}
	else {
		version = null;
		versionComparator = null;
	}

	CollectionCacheEntry entry = new CollectionCacheEntry( lce.getCollection(), persister );
	CacheKey cacheKey = new CacheKey(
			lce.getKey(),
			persister.getKeyType(),
			persister.getRole(),
			session.getEntityMode(),
			session.getFactory()
	);
	boolean put = persister.getCache().put(
			cacheKey,
			persister.getCacheEntryStructure().structure(entry),
			session.getTimestamp(),
			version,
			versionComparator,
			factory.getSettings().isMinimalPutsEnabled() && session.getCacheMode()!= CacheMode.REFRESH
	);

	if ( put && factory.getStatistics().isStatisticsEnabled() ) {
		factory.getStatisticsImplementor().secondLevelCachePut( persister.getCache().getRegionName() );
	}
}
 
Example 17
Source File: OneToOneType.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public boolean isNull(Object owner, SessionImplementor session) {
	
	if ( propertyName != null ) {
		
		EntityPersister ownerPersister = session.getFactory()
				.getEntityPersister(entityName); 
		Serializable id = session.getContextEntityIdentifier(owner);

		EntityKey entityKey = new EntityKey( id, ownerPersister, session.getEntityMode() );
		
		return session.getPersistenceContext()
				.isPropertyNull( entityKey, getPropertyName() );
		
	}
	else {
		return false;
	}

}
 
Example 18
Source File: EntityType.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected boolean isNotEmbedded(SessionImplementor session) {
	return !isEmbeddedInXML && session.getEntityMode()==EntityMode.DOM4J;
}
 
Example 19
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Attempts to load the entity from the second-level cache.
 *
 * @param event The load event
 * @param persister The persister for the entity being requested for load
 * @param options The load options.
 * @return The entity from the second-level cache, or null.
 * @throws HibernateException
 */
protected Object loadFromSecondLevelCache(
		final LoadEvent event,
		final EntityPersister persister,
		final LoadEventListener.LoadType options) throws HibernateException {
	
	final SessionImplementor source = event.getSession();
	
	final boolean useCache = persister.hasCache() && 
		source.getCacheMode().isGetEnabled() && 
		event.getLockMode().lessThan(LockMode.READ);
	
	if (useCache) {
		
		final SessionFactoryImplementor factory = source.getFactory();
		
		final CacheKey ck = new CacheKey( 
				event.getEntityId(), 
				persister.getIdentifierType(), 
				persister.getRootEntityName(),
				source.getEntityMode(), 
				source.getFactory()
			);
		Object ce = persister.getCache()
			.get( ck, source.getTimestamp() );
		
		if ( factory.getStatistics().isStatisticsEnabled() ) {
			if (ce==null) {
				factory.getStatisticsImplementor().secondLevelCacheMiss( 
					persister.getCache().getRegionName() 
				);
			}
			else {
				factory.getStatisticsImplementor().secondLevelCacheHit( 
					persister.getCache().getRegionName() 
				);
			}
		}

		if ( ce != null ) {

			CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure()
					.destructure(ce, factory);
		
			// Entity was found in second-level cache...
			return assembleCacheEntry(
					entry,
					event.getEntityId(),
					persister,
					event
				);
		}
	}
	
	return null;
}
 
Example 20
Source File: EntityDeleteAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void execute() throws HibernateException {
	Serializable id = getId();
	EntityPersister persister = getPersister();
	SessionImplementor session = getSession();
	Object instance = getInstance();

	boolean veto = preDelete();

	Object version = this.version;
	if ( persister.isVersionPropertyGenerated() ) {
		// we need to grab the version value from the entity, otherwise
		// we have issues with generated-version entities that may have
		// multiple actions queued during the same flush
		version = persister.getVersion( instance, session.getEntityMode() );
	}

	final CacheKey ck;
	if ( persister.hasCache() ) {
		ck = new CacheKey( 
				id, 
				persister.getIdentifierType(), 
				persister.getRootEntityName(), 
				session.getEntityMode(), 
				session.getFactory() 
			);
		lock = persister.getCache().lock(ck, version);
	}
	else {
		ck = null;
	}

	if ( !isCascadeDeleteEnabled && !veto ) {
		persister.delete( id, version, instance, session );
	}
	
	//postDelete:
	// After actually deleting a row, record the fact that the instance no longer 
	// exists on the database (needed for identity-column key generation), and
	// remove it from the session cache
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	EntityEntry entry = persistenceContext.removeEntry( instance );
	if ( entry == null ) {
		throw new AssertionFailure( "possible nonthreadsafe access to session" );
	}
	entry.postDelete();

	EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
	persistenceContext.removeEntity(key);
	persistenceContext.removeProxy(key);
	
	if ( persister.hasCache() ) persister.getCache().evict(ck);

	postDelete();

	if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
		getSession().getFactory().getStatisticsImplementor()
				.deleteEntity( getPersister().getEntityName() );
	}
}