org.hibernate.WrongClassException Java Examples

The following examples show how to use org.hibernate.WrongClassException. 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 5 votes vote down vote up
/**
 * Determine the concrete class of an instance in the <tt>ResultSet</tt>
 */
private String getInstanceClass(
        final ResultSet rs,
        final int i,
        final Loadable persister,
        final Serializable id,
        final SessionImplementor session) 
throws HibernateException, SQLException {

	if ( persister.hasSubclasses() ) {

		// Code to handle subclasses of topClass
		Object discriminatorValue = persister.getDiscriminatorType().nullSafeGet(
				rs,
				getEntityAliases()[i].getSuffixedDiscriminatorAlias(),
				session,
				null
			);

		final String result = persister.getSubclassForDiscriminatorValue( discriminatorValue );

		if ( result == null ) {
			//woops we got an instance of another class hierarchy branch
			throw new WrongClassException( 
					"Discriminator: " + discriminatorValue,
					id,
					persister.getEntityName() 
				);
		}

		return result;

	}
	else {
		return persister.getEntityName();
	}
}
 
Example #2
Source File: Loader.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * The entity instance is already in the session cache
 */
private void instanceAlreadyLoaded(
        final ResultSet rs,
        final int i,
        final Loadable persister,
        final EntityKey key,
        final Object object,
        final LockMode lockMode,
        final SessionImplementor session) 
throws HibernateException, SQLException {

	if ( !persister.isInstance( object, session.getEntityMode() ) ) {
		throw new WrongClassException( 
				"loaded object was of wrong class " + object.getClass(), 
				key.getIdentifier(), 
				persister.getEntityName() 
			);
	}

	if ( LockMode.NONE != lockMode && upgradeLocks() ) { //no point doing this if NONE was requested

		final boolean isVersionCheckNeeded = persister.isVersioned() &&
				session.getPersistenceContext().getEntry(object)
						.getLockMode().lessThan( lockMode );
		// we don't need to worry about existing version being uninitialized
		// because this block isn't called by a re-entrant load (re-entrant
		// loads _always_ have lock mode NONE)
		if (isVersionCheckNeeded) {
			//we only check the version when _upgrading_ lock modes
			checkVersion( i, persister, key.getIdentifier(), object, rs, session );
			//we need to upgrade the lock mode to the mode requested
			session.getPersistenceContext().getEntry(object)
					.setLockMode(lockMode);
		}
	}
}
 
Example #3
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The entity instance is already in the session cache
 */
private void instanceAlreadyLoaded(
		final ResultSet rs,
		final int i,
		final Loadable persister,
		final EntityKey key,
		final Object object,
		final LockMode requestedLockMode,
		final SharedSessionContractImplementor session)
		throws HibernateException, SQLException {
	if ( !persister.isInstance( object ) ) {
		throw new WrongClassException(
				"loaded object was of wrong class " + object.getClass(),
				key.getIdentifier(),
				persister.getEntityName()
		);
	}

	if ( LockMode.NONE != requestedLockMode && upgradeLocks() ) { //no point doing this if NONE was requested
		final EntityEntry entry = session.getPersistenceContext().getEntry( object );
		if ( entry.getLockMode().lessThan( requestedLockMode ) ) {
			//we only check the version when _upgrading_ lock modes
			if ( persister.isVersioned() ) {
				checkVersion( i, persister, key.getIdentifier(), object, rs, session );
			}
			//we need to upgrade the lock mode to the mode requested
			entry.setLockMode( requestedLockMode );
		}
	}
}
 
Example #4
Source File: Loader.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Determine the concrete class of an instance in the <tt>ResultSet</tt>
 */
private String getInstanceClass(
		final ResultSet rs,
		final int i,
		final Loadable persister,
		final Serializable id,
		final SharedSessionContractImplementor session) throws HibernateException, SQLException {

	if ( persister.hasSubclasses() ) {

		// Code to handle subclasses of topClass
		final Object discriminatorValue = persister.getDiscriminatorType().nullSafeGet(
				rs,
				getEntityAliases()[i].getSuffixedDiscriminatorAlias(),
				session,
				null
		);

		final String result = persister.getSubclassForDiscriminatorValue( discriminatorValue );

		if ( result == null ) {
			//woops we got an instance of another class hierarchy branch
			throw new WrongClassException(
					"Discriminator: " + discriminatorValue,
					id,
					persister.getEntityName()
			);
		}

		return result;

	}
	else {
		return persister.getEntityName();
	}
}
 
Example #5
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String getConcreteEntityTypeName(
		ResultSet resultSet,
		ResultSetProcessingContext context,
		EntityKey entityKey) {
	final Loadable loadable = (Loadable) entityReference.getEntityPersister();
	if ( ! loadable.hasSubclasses() ) {
		return entityReference.getEntityPersister().getEntityName();
	}

	final Object discriminatorValue;
	try {
		discriminatorValue = loadable.getDiscriminatorType().nullSafeGet(
				resultSet,
				entityReferenceAliases.getColumnAliases().getSuffixedDiscriminatorAlias(),
				context.getSession(),
				null
		);
	}
	catch (SQLException e) {
		throw context.getSession().getFactory().getServiceRegistry().getService( JdbcServices.class ).getSqlExceptionHelper().convert(
				e,
				"Could not read discriminator value from ResultSet"
		);
	}

	final String result = loadable.getSubclassForDiscriminatorValue( discriminatorValue );

	if ( result == null ) {
		// whoops! we got an instance of another class hierarchy branch
		throw new WrongClassException(
				"Discriminator: " + discriminatorValue,
				entityKey.getIdentifier(),
				entityReference.getEntityPersister().getEntityName()
		);
	}

	return result;
}
 
Example #6
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 #7
Source File: DefaultMergeEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void entityIsDetached(MergeEvent event, Map copyCache) {
	
	log.trace("merging detached instance");
	
	final Object entity = event.getEntity();
	final EventSource source = event.getSession();

	final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
	final String entityName = persister.getEntityName();
		
	Serializable id = event.getRequestedId();
	if ( id == null ) {
		id = persister.getIdentifier( entity, source.getEntityMode() );
	}
	else {
		// check that entity id = requestedId
		Serializable entityId = persister.getIdentifier( entity, source.getEntityMode() );
		if ( !persister.getIdentifierType().isEqual( id, entityId, source.getEntityMode(), source.getFactory() ) ) {
			throw new HibernateException( "merge requested with id not matching id of passed entity" );
		}
	}
	
	String previousFetchProfile = source.getFetchProfile();
	source.setFetchProfile("merge");
	//we must clone embedded composite identifiers, or 
	//we will get back the same instance that we pass in
	final Serializable clonedIdentifier = (Serializable) persister.getIdentifierType()
			.deepCopy( id, source.getEntityMode(), source.getFactory() );
	final Object result = source.get(entityName, clonedIdentifier);
	source.setFetchProfile(previousFetchProfile);
	
	if ( result == null ) {
		//TODO: we should throw an exception if we really *know* for sure  
		//      that this is a detached instance, rather than just assuming
		//throw new StaleObjectStateException(entityName, id);
		
		// we got here because we assumed that an instance
		// with an assigned id was detached, when it was
		// really persistent
		entityIsTransient(event, copyCache);
	}
	else {
		copyCache.put(entity, result); //before cascade!

		final Object target = source.getPersistenceContext().unproxy(result);
		if ( target == entity ) {
			throw new AssertionFailure("entity was not detached");
		}
		else if ( !source.getEntityName(target).equals(entityName) ) {
			throw new WrongClassException(
					"class of the given object did not match class of persistent copy",
					event.getRequestedId(),
					entityName
				);
		}
		else if ( isVersionChanged( entity, source, persister, target ) ) {
			if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
				source.getFactory().getStatisticsImplementor()
						.optimisticFailure( entityName );
			}
			throw new StaleObjectStateException( entityName, id );
		}

		// cascade first, so that all unsaved objects get their 
		// copy created before we actually copy
		cascadeOnMerge(source, persister, entity, copyCache);
		copyValues(persister, entity, target, source, copyCache);
		
		//copyValues works by reflection, so explicitly mark the entity instance dirty
		markInterceptorDirty( entity, target );
		
		event.setResult(result);
	}

}
 
Example #8
Source File: HibernateObjectRetrievalFailureException.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #9
Source File: HibernateObjectRetrievalFailureException.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #10
Source File: HibernateObjectRetrievalFailureException.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #11
Source File: HibernateObjectRetrievalFailureException.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #12
Source File: DefaultMergeEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void entityIsDetached(MergeEvent event, Map copyCache) {

		LOG.trace( "Merging detached instance" );

		final Object entity = event.getEntity();
		final EventSource source = event.getSession();

		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
		final String entityName = persister.getEntityName();

		Serializable id = event.getRequestedId();
		if ( id == null ) {
			id = persister.getIdentifier( entity, source );
		}
		else {
			// check that entity id = requestedId
			Serializable entityId = persister.getIdentifier( entity, source );
			if ( !persister.getIdentifierType().isEqual( id, entityId, source.getFactory() ) ) {
				throw new HibernateException( "merge requested with id not matching id of passed entity" );
			}
		}

		String previousFetchProfile = source.getLoadQueryInfluencers().getInternalFetchProfile();
		source.getLoadQueryInfluencers().setInternalFetchProfile( "merge" );
		//we must clone embedded composite identifiers, or
		//we will get back the same instance that we pass in
		final Serializable clonedIdentifier = (Serializable) persister.getIdentifierType()
				.deepCopy( id, source.getFactory() );
		final Object result = source.get( entityName, clonedIdentifier );
		source.getLoadQueryInfluencers().setInternalFetchProfile( previousFetchProfile );

		if ( result == null ) {
			//TODO: we should throw an exception if we really *know* for sure
			//      that this is a detached instance, rather than just assuming
			//throw new StaleObjectStateException(entityName, id);

			// we got here because we assumed that an instance
			// with an assigned id was detached, when it was
			// really persistent
			entityIsTransient( event, copyCache );
		}
		else {
			( (MergeContext) copyCache ).put( entity, result, true ); //before cascade!

			final Object target = source.getPersistenceContext().unproxy( result );
			if ( target == entity ) {
				throw new AssertionFailure( "entity was not detached" );
			}
			else if ( !source.getEntityName( target ).equals( entityName ) ) {
				throw new WrongClassException(
						"class of the given object did not match class of persistent copy",
						event.getRequestedId(),
						entityName
				);
			}
			else if ( isVersionChanged( entity, source, persister, target ) ) {
				if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
					source.getFactory().getStatistics().optimisticFailure( entityName );
				}
				throw new StaleObjectStateException( entityName, id );
			}

			// cascade first, so that all unsaved objects get their
			// copy created before we actually copy
			cascadeOnMerge( source, persister, entity, copyCache );
			copyValues( persister, entity, target, source, copyCache );

			//copyValues works by reflection, so explicitly mark the entity instance dirty
			markInterceptorDirty( entity, target, persister );

			event.setResult( result );
		}

	}
 
Example #13
Source File: EntityReferenceInitializerImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void hydrateEntityState(ResultSet resultSet, ResultSetProcessingContextImpl context) {
	final EntityReferenceProcessingState processingState = context.getProcessingState( entityReference );

	// If there is no identifier for this entity reference for this row, nothing to do
	if ( processingState.isMissingIdentifier() ) {
		handleMissingIdentifier( context );
		return;
	}

	// make sure we have the EntityKey
	final EntityKey entityKey = processingState.getEntityKey();
	if ( entityKey == null ) {
		handleMissingIdentifier( context );
		return;
	}

	// Have we already hydrated this entity's state?
	if ( processingState.getEntityInstance() != null ) {
		return;
	}


	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// In getting here, we know that:
	// 		1) We need to hydrate the entity state
	//		2) We have a valid EntityKey for the entity

	// see if we have an existing entry in the session for this EntityKey
	final Object existing = context.getSession().getEntityUsingInterceptor( entityKey );
	if ( existing != null ) {
		// It is previously associated with the Session, perform some checks
		if ( ! entityReference.getEntityPersister().isInstance( existing ) ) {
			throw new WrongClassException(
					"loaded object was of wrong class " + existing.getClass(),
					entityKey.getIdentifier(),
					entityReference.getEntityPersister().getEntityName()
			);
		}
		checkVersion( resultSet, context, entityKey, existing );

		// use the existing association as the hydrated state
		processingState.registerEntityInstance( existing );
		//context.registerHydratedEntity( entityReference, entityKey, existing );
		return;
	}

	// Otherwise, we need to load it from the ResultSet...

	// determine which entity instance to use.  Either the supplied one, or instantiate one
	Object entityInstance = null;
	if ( isReturn &&
			context.shouldUseOptionalEntityInformation() &&
			context.getQueryParameters().getOptionalObject() != null ) {
		final EntityKey optionalEntityKey = ResultSetProcessorHelper.getOptionalObjectKey(
				context.getQueryParameters(),
				context.getSession()
		);
		if ( optionalEntityKey != null && optionalEntityKey.equals( entityKey ) ) {
			entityInstance = context.getQueryParameters().getOptionalObject();
		}
	}

	final String concreteEntityTypeName = getConcreteEntityTypeName( resultSet, context, entityKey );
	if ( entityInstance == null ) {
		entityInstance = context.getSession().instantiate( concreteEntityTypeName, entityKey.getIdentifier() );
	}
	processingState.registerEntityInstance( entityInstance );

	// need to hydrate it.
	// grab its state from the ResultSet and keep it in the Session
	// (but don't yet initialize the object itself)
	// note that we acquire LockMode.READ even if it was not requested
	log.trace( "hydrating entity state" );
	final LockMode requestedLockMode = context.resolveLockMode( entityReference );
	final LockMode lockModeToAcquire = requestedLockMode == LockMode.NONE
			? LockMode.READ
			: requestedLockMode;

	loadFromResultSet(
			resultSet,
			context,
			entityInstance,
			concreteEntityTypeName,
			entityKey,
			lockModeToAcquire
	);
}
 
Example #14
Source File: HibernateObjectRetrievalFailureException.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #15
Source File: HibernateObjectRetrievalFailureException.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #16
Source File: HibernateObjectRetrievalFailureException.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
 
Example #17
Source File: HibernateObjectRetrievalFailureException.java    From java-technology-stack with MIT License 4 votes vote down vote up
public HibernateObjectRetrievalFailureException(WrongClassException ex) {
	super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}