Java Code Examples for org.hibernate.persister.entity.EntityPersister#isInstance()

The following examples show how to use org.hibernate.persister.entity.EntityPersister#isInstance() . 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: ResultSetProcessorHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public EntityKey interpretEntityKey(
		SharedSessionContractImplementor session,
		String optionalEntityName,
		Serializable optionalId,
		Object optionalObject) {
	if ( optionalEntityName != null ) {
		final EntityPersister entityPersister;
		if ( optionalObject != null ) {
			entityPersister = session.getEntityPersister( optionalEntityName, optionalObject );
		}
		else {
			entityPersister = session.getFactory().getMetamodel().entityPersister( optionalEntityName );
		}
		if ( entityPersister.isInstance( optionalId )
				&& !entityPersister.getEntityMetamodel().getIdentifierProperty().isVirtual()
				&& entityPersister.getEntityMetamodel().getIdentifierProperty().isEmbedded() ) {
			// non-encapsulated composite identifier
			final Serializable identifierState = ((CompositeType) entityPersister.getIdentifierType()).getPropertyValues(
					optionalId,
					session
			);
			return session.generateEntityKey( identifierState, entityPersister );
		}
		else {
			return session.generateEntityKey( optionalId, entityPersister );
		}
	}
	else {
		return null;
	}
}
 
Example 2
Source File: EntityPrinter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Renders an entity to a string.
 *
 * @param entityName the entity name
 * @param entity an actual entity object, not a proxy!
 *
 * @return the entity rendered to a string
 */
public String toString(String entityName, Object entity) throws HibernateException {
	EntityPersister entityPersister = factory.getEntityPersister( entityName );

	if ( entityPersister == null || !entityPersister.isInstance( entity ) ) {
		return entity.getClass().getName();
	}

	Map<String, String> result = new HashMap<String, String>();

	if ( entityPersister.hasIdentifierProperty() ) {
		result.put(
				entityPersister.getIdentifierPropertyName(),
				entityPersister.getIdentifierType().toLoggableString(
						entityPersister.getIdentifier( entity ),
						factory
				)
		);
	}

	Type[] types = entityPersister.getPropertyTypes();
	String[] names = entityPersister.getPropertyNames();
	Object[] values = entityPersister.getPropertyValues( entity );
	for ( int i = 0; i < types.length; i++ ) {
		if ( !names[i].startsWith( "_" ) ) {
			String strValue = values[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ?
					values[i].toString() :
					types[i].toLoggableString( values[i], factory );
			result.put( names[i], strValue );
		}
	}
	return entityName + result.toString();
}
 
Example 3
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Attempts to locate the entity in the session-level cache.
 * <p/>
 * If allowed to return nulls, then if the entity happens to be found in
 * the session cache, we check the entity type for proper handling
 * of entity hierarchies.
 * <p/>
 * If checkDeleted was set to true, then if the entity is found in the
 * session-level cache, it's current status within the session cache
 * is checked to see if it has previously been scheduled for deletion.
 *
 * @param event The load event
 * @param keyToLoad The EntityKey representing the entity to be loaded.
 * @param options The load options.
 *
 * @return The entity from the session-level cache, or null.
 *
 * @throws HibernateException Generally indicates problems applying a lock-mode.
 */
protected Object loadFromSessionCache(
		final LoadEvent event,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) throws HibernateException {

	SessionImplementor session = event.getSession();
	Object old = session.getEntityUsingInterceptor( keyToLoad );

	if ( old != null ) {
		// this object was already loaded
		EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
		if ( options.isCheckDeleted() ) {
			Status status = oldEntry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return REMOVED_ENTITY_MARKER;
			}
		}
		if ( options.isAllowNulls() ) {
			final EntityPersister persister = event.getSession()
					.getFactory()
					.getEntityPersister( keyToLoad.getEntityName() );
			if ( !persister.isInstance( old ) ) {
				return INCONSISTENT_RTN_CLASS_MARKER;
			}
		}
		upgradeLock( old, oldEntry, event.getLockOptions(), event.getSession() );
	}

	return old;
}
 
Example 4
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Object processCachedEntry(
		final LoadEvent event,
		final EntityPersister persister,
		final Object ce,
		final SessionImplementor source,
		final EntityKey entityKey) {

	CacheEntry entry = (CacheEntry) persister.getCacheEntryStructure().destructure( ce, source.getFactory() );
	if(entry.isReferenceEntry()) {
		if( event.getInstanceToLoad() != null ) {
			throw new HibernateException(
					"Attempt to load entity [%s] from cache using provided object instance, but cache " +
							"is storing references: "+ event.getEntityId());
		}
		else {
			return convertCacheReferenceEntryToEntity( (ReferenceCacheEntryImpl) entry,
					event.getSession(), entityKey);
		}
	}
	else {
		Object entity = convertCacheEntryToEntity( entry, event.getEntityId(), persister, event, entityKey );

		if ( !persister.isInstance( entity ) ) {
			throw new WrongClassException(
					"loaded object was of wrong class " + entity.getClass(),
					event.getEntityId(),
					persister.getEntityName()
			);
		}

		return entity;
	}
}
 
Example 5
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Attempts to locate the entity in the session-level cache.
 * <p/>
 * If allowed to return nulls, then if the entity happens to be found in
 * the session cache, we check the entity type for proper handling
 * of entity hierarchies.
 * <p/>
 * If checkDeleted was set to true, then if the entity is found in the
 * session-level cache, it's current status within the session cache
 * is checked to see if it has previously been scheduled for deletion.
 *
 * @param event The load event
 * @param keyToLoad The EntityKey representing the entity to be loaded.
 * @param options The load options.
 * @return The entity from the session-level cache, or null.
 * @throws HibernateException Generally indicates problems applying a lock-mode.
 */
protected Object loadFromSessionCache(
		final LoadEvent event,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) throws HibernateException {
	
	SessionImplementor session = event.getSession();
	Object old = session.getEntityUsingInterceptor( keyToLoad );

	if ( old != null ) {
		// this object was already loaded
		EntityEntry oldEntry = session.getPersistenceContext().getEntry( old );
		if ( options.isCheckDeleted() ) {
			Status status = oldEntry.getStatus();
			if ( status == Status.DELETED || status == Status.GONE ) {
				return REMOVED_ENTITY_MARKER;
			}
		}
		if ( options.isAllowNulls() ) {
			EntityPersister persister = event.getSession().getFactory().getEntityPersister( event.getEntityClassName() );
			if ( ! persister.isInstance( old, event.getSession().getEntityMode() ) ) {
				return INCONSISTENT_RTN_CLASS_MARKER;
			}
		}
		upgradeLock( old, oldEntry, event.getLockMode(), session );
	}

	return old;
}
 
Example 6
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object getCollectionOwner(Serializable key, CollectionPersister collectionPersister) throws MappingException {
	// todo : we really just need to add a split in the notions of:
	//		1) collection key
	//		2) collection owner key
	// these 2 are not always the same.  Same is true in the case of ToOne associations with property-ref...
	final EntityPersister ownerPersister = collectionPersister.getOwnerEntityPersister();
	if ( ownerPersister.getIdentifierType().getReturnedClass().isInstance( key ) ) {
		return getEntity( session.generateEntityKey( key, collectionPersister.getOwnerEntityPersister() ) );
	}

	// we have a property-ref type mapping for the collection key.  But that could show up a few ways here...
	//
	//		1) The incoming key could be the entity itself...
	if ( ownerPersister.isInstance( key ) ) {
		final Serializable owenerId = ownerPersister.getIdentifier( key, session );
		if ( owenerId == null ) {
			return null;
		}
		return getEntity( session.generateEntityKey( owenerId, ownerPersister ) );
	}

	final CollectionType collectionType = collectionPersister.getCollectionType();

	//		2) The incoming key is most likely the collection key which we need to resolve to the owner key
	//			find the corresponding owner instance
	//			a) try by EntityUniqueKey
	if ( collectionType.getLHSPropertyName() != null ) {
		final Object owner = getEntity(
				new EntityUniqueKey(
						ownerPersister.getEntityName(),
						collectionType.getLHSPropertyName(),
						key,
						collectionPersister.getKeyType(),
						ownerPersister.getEntityMode(),
						session.getFactory()
				)
		);
		if ( owner != null ) {
			return owner;
		}

		//		b) try by EntityKey, which means we need to resolve owner-key -> collection-key
		//			IMPL NOTE : yes if we get here this impl is very non-performant, but PersistenceContext
		//					was never designed to handle this case; adding that capability for real means splitting
		//					the notions of:
		//						1) collection key
		//						2) collection owner key
		// 					these 2 are not always the same (same is true in the case of ToOne associations with
		// 					property-ref).  That would require changes to (at least) CollectionEntry and quite
		//					probably changes to how the sql for collection initializers are generated
		//
		//			We could also possibly see if the referenced property is a natural id since we already have caching
		//			in place of natural id snapshots.  BUt really its better to just do it the right way ^^ if we start
		// 			going that route
		final Serializable ownerId = ownerPersister.getIdByUniqueKey( key, collectionType.getLHSPropertyName(), session );
		return getEntity( session.generateEntityKey( ownerId, ownerPersister ) );
	}

	// as a last resort this is what the old code did...
	return getEntity( session.generateEntityKey( key, collectionPersister.getOwnerEntityPersister() ) );
}