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

The following examples show how to use org.hibernate.persister.entity.EntityPersister#setIdentifier() . 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: DefaultReactiveLoadEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Performs the load of an entity.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 *
 * @return The loaded entity.
 */
private CompletionStage<Object> load( LoadEvent event, EntityPersister persister, EntityKey keyToLoad, LoadType options) {
	final EventSource session = event.getSession();
	if ( event.getInstanceToLoad() != null ) {
		if ( session.getPersistenceContextInternal().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
							MessageHelper.infoString( persister, event.getEntityId(), session.getFactory() ) );
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), session );
	}

	return doLoad( event, persister, keyToLoad, options )
			.thenApply( optional -> {
				boolean isOptionalInstance = event.getInstanceToLoad() != null;
				if ( optional==null && ( !options.isAllowNulls() || isOptionalInstance ) ) {
					throwEntityNotFound( session, event.getEntityClassName(), event.getEntityId() );
				}
				else if ( isOptionalInstance && optional != event.getInstanceToLoad() ) {
					throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
				}
				return optional;
			} );
}
 
Example 2
Source File: StatelessSessionImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Serializable insert(String entityName, Object entity) {
	errorIfClosed();
	EntityPersister persister = getEntityPersister(entityName, entity);
	Serializable id = persister.getIdentifierGenerator().generate(this, entity);
	Object[] state = persister.getPropertyValues(entity, EntityMode.POJO);
	if ( persister.isVersioned() ) {
		boolean substitute = Versioning.seedVersion(state, persister.getVersionProperty(), persister.getVersionType(), this);
		if ( substitute ) {
			persister.setPropertyValues( entity, state, EntityMode.POJO );
		}
	}
	if ( id == IdentifierGeneratorFactory.POST_INSERT_INDICATOR ) {
		id = persister.insert(state, entity, this);
	}
	else {
		persister.insert(id, state, entity, this);
	}
	persister.setIdentifier(entity, id, EntityMode.POJO);
	return id;
}
 
Example 3
Source File: StatelessSessionImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Serializable insert(String entityName, Object entity) {
	checkOpen();
	EntityPersister persister = getEntityPersister( entityName, entity );
	Serializable id = persister.getIdentifierGenerator().generate( this, entity );
	Object[] state = persister.getPropertyValues( entity );
	if ( persister.isVersioned() ) {
		boolean substitute = Versioning.seedVersion(
				state,
				persister.getVersionProperty(),
				persister.getVersionType(),
				this
		);
		if ( substitute ) {
			persister.setPropertyValues( entity, state );
		}
	}
	if ( id == IdentifierGeneratorHelper.POST_INSERT_INDICATOR ) {
		id = persister.insert( state, entity, this );
	}
	else {
		persister.insert( id, state, entity, this );
	}
	persister.setIdentifier( entity, id, this );
	return id;
}
 
Example 4
Source File: EntityIdentityInsertAction.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void execute() throws HibernateException {
	
	final EntityPersister persister = getPersister();
	final SessionImplementor session = getSession();
	final Object instance = getInstance();
	
	boolean veto = preInsert();

	// Don't need to lock the cache here, since if someone
	// else inserted the same pk first, the insert would fail

	if ( !veto ) {
		generatedId = persister.insert( state, instance, session );
		if ( persister.hasInsertGeneratedProperties() ) {
			persister.processInsertGeneratedProperties( generatedId, instance, state, session );
		}
		//need to do that here rather than in the save event listener to let
		//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
		persister.setIdentifier( instance, generatedId, session.getEntityMode() );
	}


	//TODO: this bit actually has to be called after all cascades!
	//      but since identity insert is called *synchronously*,
	//      instead of asynchronously as other actions, it isn't
	/*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
		cacheEntry = new CacheEntry(object, persister, session);
		persister.getCache().insert(generatedId, cacheEntry);
	}*/
	
	postInsert();

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

}
 
Example 5
Source File: DefaultLoadEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Perfoms the load of an entity.
 *
 * @return The loaded entity.
 * @throws HibernateException
 */
protected Object load(
	final LoadEvent event, 
	final EntityPersister persister, 
	final EntityKey keyToLoad, 
	final LoadEventListener.LoadType options)
throws HibernateException {

	if ( event.getInstanceToLoad() != null ) {
		if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
					MessageHelper.infoString( persister, event.getEntityId(), event.getSession().getFactory() )
				);
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession().getEntityMode() );
	}

	Object entity = doLoad(event, persister, keyToLoad, options);
	
	boolean isOptionalInstance = event.getInstanceToLoad() != null;
	
	if ( !options.isAllowNulls() || isOptionalInstance ) {
		if ( entity == null ) {
			event.getSession().getFactory().getEntityNotFoundDelegate().handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
		}
	}

	if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
		throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
	}

	return entity;
}
 
Example 6
Source File: DefaultUpdateEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * If the user specified an id, assign it to the instance and use that, 
 * otherwise use the id already assigned to the instance
 */
protected Serializable getUpdateId(Object entity, EntityPersister persister, Serializable requestedId, EntityMode entityMode)
throws HibernateException {

	if ( requestedId==null ) {
		return super.getUpdateId(entity, persister, requestedId, entityMode);
	}
	else {
		persister.setIdentifier(entity, requestedId, entityMode);
		return requestedId;
	}
}
 
Example 7
Source File: IdTransferringMergeEventListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Hibernate 3.1 implementation of ID transferral.
 */
@Override
protected void entityIsTransient(MergeEvent event, Map copyCache) {
	super.entityIsTransient(event, copyCache);
	SessionImplementor session = event.getSession();
	EntityPersister persister = session.getEntityPersister(event.getEntityName(), event.getEntity());
	// Extract id from merged copy (which is currently registered with Session).
	Serializable id = persister.getIdentifier(event.getResult(), session.getEntityMode());
	// Set id on original object (which remains detached).
	persister.setIdentifier(event.getOriginal(), id, session.getEntityMode());
}
 
Example 8
Source File: DefaultMergeEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void entityIsTransient(MergeEvent event, Map copyCache) {

		LOG.trace( "Merging transient instance" );

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

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

		final Serializable id = persister.hasIdentifierProperty() ?
				persister.getIdentifier( entity, source ) :
				null;
		if ( copyCache.containsKey( entity ) ) {
			persister.setIdentifier( copyCache.get( entity ), id, source );
		}
		else {
			( (MergeContext) copyCache ).put( entity, source.instantiate( persister, id ), true ); //before cascade!
		}
		final Object copy = copyCache.get( entity );

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
		super.cascadeBeforeSave( source, persister, entity, copyCache );
		copyValues( persister, entity, copy, source, copyCache, ForeignKeyDirection.FROM_PARENT );

		saveTransientEntity( copy, entityName, event.getRequestedId(), source, copyCache );

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		super.cascadeAfterSave( source, persister, entity, copyCache );
		copyValues( persister, entity, copy, source, copyCache, ForeignKeyDirection.TO_PARENT );

		event.setResult( copy );
	}
 
Example 9
Source File: DefaultLoadEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Performs the load of an entity.
 *
 * @param event The initiating load request event
 * @param persister The persister corresponding to the entity to be loaded
 * @param keyToLoad The key of the entity to be loaded
 * @param options The defined load options
 *
 * @return The loaded entity.
 *
 * @throws HibernateException
 */
private Object load(
		final LoadEvent event,
		final EntityPersister persister,
		final EntityKey keyToLoad,
		final LoadEventListener.LoadType options) {

	if ( event.getInstanceToLoad() != null ) {
		if ( event.getSession().getPersistenceContext().getEntry( event.getInstanceToLoad() ) != null ) {
			throw new PersistentObjectException(
					"attempted to load into an instance that was already associated with the session: " +
							MessageHelper.infoString(
									persister,
									event.getEntityId(),
									event.getSession().getFactory()
							)
			);
		}
		persister.setIdentifier( event.getInstanceToLoad(), event.getEntityId(), event.getSession() );
	}

	final Object entity = doLoad( event, persister, keyToLoad, options );

	boolean isOptionalInstance = event.getInstanceToLoad() != null;

	if ( entity == null && ( !options.isAllowNulls() || isOptionalInstance ) ) {
		event.getSession()
				.getFactory()
				.getEntityNotFoundDelegate()
				.handleEntityNotFound( event.getEntityClassName(), event.getEntityId() );
	}
	else if ( isOptionalInstance && entity != event.getInstanceToLoad() ) {
		throw new NonUniqueObjectException( event.getEntityId(), event.getEntityClassName() );
	}

	return entity;
}
 
Example 10
Source File: DefaultUpdateEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If the user specified an id, assign it to the instance and use that, 
 * otherwise use the id already assigned to the instance
 */
protected Serializable getUpdateId(
		Object entity,
		EntityPersister persister,
		Serializable requestedId,
		SessionImplementor session) throws HibernateException {
	if ( requestedId == null ) {
		return super.getUpdateId( entity, persister, requestedId, session );
	}
	else {
		persister.setIdentifier( entity, requestedId, session );
		return requestedId;
	}
}
 
Example 11
Source File: IdTransferringMergeEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Hibernate 3.1 implementation of ID transferral.
 */
@Override
protected void entityIsTransient(MergeEvent event, Map copyCache) {
	super.entityIsTransient(event, copyCache);
	SessionImplementor session = event.getSession();
	EntityPersister persister = session.getEntityPersister(event.getEntityName(), event.getEntity());
	// Extract id from merged copy (which is currently registered with Session).
	Serializable id = persister.getIdentifier(event.getResult(), session.getEntityMode());
	// Set id on original object (which remains detached).
	persister.setIdentifier(event.getOriginal(), id, session.getEntityMode());
}
 
Example 12
Source File: DefaultPersistEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle the given create event.
 *
 * @param event The create event to be handled.
 *
 */
public void onPersist(PersistEvent event, Map createCache) throws HibernateException {
	final SessionImplementor source = event.getSession();
	final Object object = event.getObject();

	final Object entity;
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			if ( li.getSession() == source ) {
				return; //NOTE EARLY EXIT!
			}
			else {
				throw new PersistentObjectException( "uninitialized proxy passed to persist()" );
			}
		}
		entity = li.getImplementation();
	}
	else {
		entity = object;
	}

	final String entityName;
	if ( event.getEntityName() != null ) {
		entityName = event.getEntityName();
	}
	else {
		entityName = source.bestGuessEntityName( entity );
		event.setEntityName( entityName );
	}

	final EntityEntry entityEntry = source.getPersistenceContext().getEntry( entity );
	EntityState entityState = getEntityState( entity, entityName, entityEntry, source );
	if ( entityState == EntityState.DETACHED ) {
		// JPA 2, in its version of a "foreign generated", allows the id attribute value
		// to be manually set by the user, even though this manual value is irrelevant.
		// The issue is that this causes problems with the Hibernate unsaved-value strategy
		// which comes into play here in determining detached/transient state.
		//
		// Detect if we have this situation and if so null out the id value and calculate the
		// entity state again.

		// NOTE: entityEntry must be null to get here, so we cannot use any of its values
		EntityPersister persister = source.getFactory().getEntityPersister( entityName );
		if ( ForeignGenerator.class.isInstance( persister.getIdentifierGenerator() ) ) {
			if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) {
				LOG.debug( "Resetting entity id attribute to null for foreign generator" );
			}
			persister.setIdentifier( entity, null, source );
			entityState = getEntityState( entity, entityName, entityEntry, source );
		}
	}

	switch ( entityState ) {
		case DETACHED: {
			throw new PersistentObjectException(
					"detached entity passed to persist: " +
							getLoggableName( event.getEntityName(), entity )
			);
		}
		case PERSISTENT: {
			entityIsPersistent( event, createCache );
			break;
		}
		case TRANSIENT: {
			entityIsTransient( event, createCache );
			break;
		}
		case DELETED: {
			entityEntry.setStatus( Status.MANAGED );
			entityEntry.setDeletedState( null );
			event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() );
			entityIsDeleted( event, createCache );
			break;
		}
		default: {
			throw new ObjectDeletedException(
					"deleted entity passed to persist",
					null,
					getLoggableName( event.getEntityName(), entity )
			);
		}
	}

}
 
Example 13
Source File: AbstractSaveEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Prepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param anything Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable performSave(
		Object entity,
		Serializable id,
		EntityPersister persister,
		boolean useIdentityColumn,
		Object anything,
		EventSource source,
		boolean requiresImmediateIdAccess) {

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev( "Saving {0}", MessageHelper.infoString( persister, id, source.getFactory() ) );
	}

	final EntityKey key;
	if ( !useIdentityColumn ) {
		key = source.generateEntityKey( id, persister );
		Object old = source.getPersistenceContext().getEntity( key );
		if ( old != null ) {
			if ( source.getPersistenceContext().getEntry( old ).getStatus() == Status.DELETED ) {
				source.forceFlush( source.getPersistenceContext().getEntry( old ) );
			}
			else {
				throw new NonUniqueObjectException( id, persister.getEntityName() );
			}
		}
		persister.setIdentifier( entity, id, source );
	}
	else {
		key = null;
	}

	if ( invokeSaveLifecycle( entity, persister, source ) ) {
		return id; //EARLY EXIT
	}

	return performSaveOrReplicate(
			entity,
			key,
			persister,
			useIdentityColumn,
			anything,
			source,
			requiresImmediateIdAccess
	);
}
 
Example 14
Source File: EntityIdentityInsertAction.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void execute() throws HibernateException {
	nullifyTransientReferencesIfNotAlready();

	final EntityPersister persister = getPersister();
	final SharedSessionContractImplementor session = getSession();
	final Object instance = getInstance();

	setVeto( preInsert() );

	// Don't need to lock the cache here, since if someone
	// else inserted the same pk first, the insert would fail

	if ( !isVeto() ) {
		generatedId = persister.insert( getState(), instance, session );
		if ( persister.hasInsertGeneratedProperties() ) {
			persister.processInsertGeneratedProperties( generatedId, instance, getState(), session );
		}
		//need to do that here rather than in the save event listener to let
		//the post insert events to have a id-filled entity when IDENTITY is used (EJB3)
		persister.setIdentifier( instance, generatedId, session );
		session.getPersistenceContext().registerInsertedKey( getPersister(), generatedId );
		entityKey = session.generateEntityKey( generatedId, persister );
		session.getPersistenceContext().checkUniqueness( entityKey, getInstance() );
	}


	//TODO: this bit actually has to be called after all cascades!
	//      but since identity insert is called *synchronously*,
	//      instead of asynchronously as other actions, it isn't
	/*if ( persister.hasCache() && !persister.isCacheInvalidationRequired() ) {
		cacheEntry = new CacheEntry(object, persister, session);
		persister.getCache().insert(generatedId, cacheEntry);
	}*/

	postInsert();

	if ( session.getFactory().getStatistics().isStatisticsEnabled() && !isVeto() ) {
		session.getFactory().getStatistics().insertEntity( getPersister().getEntityName() );
	}

	markExecuted();
}
 
Example 15
Source File: DefaultReactiveMergeEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected CompletionStage<Void> entityIsTransient(MergeEvent event, MergeContext copyCache) {

		LOG.trace( "Merging transient instance" );

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

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

		final Serializable id = persister.hasIdentifierProperty()
				? persister.getIdentifier( entity, session )
				: null;

		final Object copy;
		final Object existingCopy = copyCache.get( entity );
		if ( existingCopy != null ) {
			persister.setIdentifier( copyCache.get( entity ), id, session );
			copy = existingCopy;
		}
		else {
			copy = session.instantiate( persister, id );

			//before cascade!
			copyCache.put( entity, copy, true );
		}

		// cascade first, so that all unsaved objects get their
		// copy created before we actually copy
		//cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE);
		return super.cascadeBeforeSave( session, persister, entity, copyCache )
				.thenAccept( v -> copyValues( persister, entity, copy, session, copyCache, ForeignKeyDirection.FROM_PARENT ) )
				.thenCompose( v -> saveTransientEntity( copy, entityName, event.getRequestedId(), session, copyCache ) )
				.thenCompose( v -> super.cascadeAfterSave( session, persister, entity, copyCache ) )
				.thenAccept( v -> {
					copyValues(persister, entity, copy, session, copyCache, ForeignKeyDirection.TO_PARENT);

					event.setResult(copy);

					if (copy instanceof PersistentAttributeInterceptable) {
						final PersistentAttributeInterceptable interceptable = (PersistentAttributeInterceptable) copy;
						final PersistentAttributeInterceptor interceptor = interceptable.$$_hibernate_getInterceptor();
						if (interceptor == null) {
							persister.getBytecodeEnhancementMetadata().injectInterceptor(copy, id, session);
						}
					}
				});
	}
 
Example 16
Source File: AbstractSaveEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Ppepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param anything Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 *         type of id generator used and the requiresImmediateIdAccess value
 */
protected Serializable performSave(
		Object entity,
		Serializable id,
		EntityPersister persister,
		boolean useIdentityColumn,
		Object anything,
		EventSource source,
		boolean requiresImmediateIdAccess) {

	if ( log.isTraceEnabled() ) {
		log.trace(
				"saving " +
						MessageHelper.infoString( persister, id, source.getFactory() )
		);
	}

	EntityKey key;
	if ( !useIdentityColumn ) {
		key = new EntityKey( id, persister, source.getEntityMode() );
		Object old = source.getPersistenceContext().getEntity( key );
		if ( old != null ) {
			if ( source.getPersistenceContext().getEntry( old ).getStatus() == Status.DELETED ) {
				source.forceFlush( source.getPersistenceContext().getEntry( old ) );
			}
			else {
				throw new NonUniqueObjectException( id, persister.getEntityName() );
			}
		}
		persister.setIdentifier( entity, id, source.getEntityMode() );
	}
	else {
		key = null;
	}

	if ( invokeSaveLifecycle( entity, persister, source ) ) {
		return id; //EARLY EXIT
	}

	return performSaveOrReplicate(
			entity,
			key,
			persister,
			useIdentityColumn,
			anything,
			source,
			requiresImmediateIdAccess
	);
}
 
Example 17
Source File: AbstractReactiveSaveEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Prepares the save call by checking the session caches for a pre-existing
 * entity and performing any lifecycle callbacks.
 *
 * @param entity The entity to be saved.
 * @param id The id by which to save the entity.
 * @param persister The entity's persister instance.
 * @param useIdentityColumn Is an identity column being used?
 * @param context Generally cascade-specific information.
 * @param source The session from which the event originated.
 * @param requiresImmediateIdAccess does the event context require
 * access to the identifier immediately after execution of this method (if
 * not, post-insert style id generators may be postponed if we are outside
 * a transaction).
 *
 * @return The id used to save the entity; may be null depending on the
 * type of id generator used and the requiresImmediateIdAccess value
 */
protected CompletionStage<Void> reactivePerformSave(
		Object entity,
		Serializable id,
		EntityPersister persister,
		boolean useIdentityColumn,
		C context,
		EventSource source,
		boolean requiresImmediateIdAccess) {

	if ( LOG.isTraceEnabled() ) {
		LOG.tracev( "Saving {0}", MessageHelper.infoString( persister, id, source.getFactory() ) );
	}

	final EntityKey key;
	if ( !useIdentityColumn ) {
		key = source.generateEntityKey( id, persister );
		final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
		Object old = persistenceContext.getEntity( key );
		if ( old != null ) {
			if ( persistenceContext.getEntry( old ).getStatus() == Status.DELETED ) {
				source.forceFlush( persistenceContext.getEntry( old ) );
			}
			else {
				return CompletionStages.failedFuture( new NonUniqueObjectException( id, persister.getEntityName() ) );
			}
		}
		persister.setIdentifier( entity, id, source );
	}
	else {
		key = null;
	}

	return reactivePerformSaveOrReplicate(
			entity,
			key,
			persister,
			useIdentityColumn,
			context,
			source,
			requiresImmediateIdAccess
	);
}
 
Example 18
Source File: DefaultReactivePersistEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Handle the given create event.
 *
 * @param event The create event to be handled.
 */
public CompletionStage<Void> reactiveOnPersist(PersistEvent event, IdentitySet createCache) throws HibernateException {
	final SessionImplementor source = event.getSession();
	final Object object = event.getObject();
	final Object entity;
	if ( object instanceof HibernateProxy ) {
		LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
		if ( li.isUninitialized() ) {
			if ( li.getSession() == source ) {
				return CompletionStages.nullFuture(); //NOTE EARLY EXIT!
			}
			else {
				return CompletionStages.failedFuture( new PersistentObjectException( "uninitialized proxy passed to persist()" ) );
			}
		}
		entity = li.getImplementation();
	}
	else {
		entity = object;
	}

	final String entityName;
	if ( event.getEntityName() != null ) {
		entityName = event.getEntityName();
	}
	else {
		entityName = source.bestGuessEntityName( entity );
		event.setEntityName( entityName );
	}

	final EntityEntry entityEntry = source.getPersistenceContextInternal().getEntry( entity );
	EntityState entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true );
	if ( entityState == EntityState.DETACHED ) {
		// JPA 2, in its version of a "foreign generated", allows the id attribute value
		// to be manually set by the user, even though this manual value is irrelevant.
		// The issue is that this causes problems with the Hibernate unsaved-value strategy
		// which comes into play here in determining detached/transient state.
		//
		// Detect if we have this situation and if so null out the id value and calculate the
		// entity state again.

		// NOTE: entityEntry must be null to get here, so we cannot use any of its values
		EntityPersister persister = source.getFactory().getMetamodel().entityPersister( entityName );
		if (persister.getIdentifierGenerator() instanceof ForeignGenerator) {
			if ( LOG.isDebugEnabled() && persister.getIdentifier( entity, source ) != null ) {
				LOG.debug( "Resetting entity id attribute to null for foreign generator" );
			}
			persister.setIdentifier( entity, null, source );
			entityState = EntityState.getEntityState( entity, entityName, entityEntry, source, true );
		}
	}

	switch ( entityState ) {
		case DETACHED: {
			return CompletionStages.failedFuture( new PersistentObjectException(
					"detached entity passed to persist: " +
							EventUtil.getLoggableName( event.getEntityName(), entity )
			) );
		}
		case PERSISTENT: {
			return entityIsPersistent( event, createCache );
		}
		case TRANSIENT: {
			return entityIsTransient( event, createCache );
		}
		case DELETED: {
			entityEntry.setStatus( Status.MANAGED );
			entityEntry.setDeletedState( null );
			event.getSession().getActionQueue().unScheduleDeletion( entityEntry, event.getObject() );
			return entityIsDeleted( event, createCache );
		}
		default: {
			return CompletionStages.failedFuture( new ObjectDeletedException(
					"deleted entity passed to persist",
					null,
					EventUtil.getLoggableName( event.getEntityName(), entity )
			) );
		}
	}
}