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

The following examples show how to use org.hibernate.engine.spi.SessionImplementor#getEntityPersister() . 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: ForeignKeys.java    From hibernate-reactive with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is this instance, which we know is not persistent, actually transient?
 * <p/>
 * If <tt>assumed</tt> is non-null, don't hit the database to make the determination, instead assume that
 * value; the client code must be prepared to "recover" in the case that this assumed result is incorrect.
 *
 * @param entityName The name of the entity
 * @param entity The entity instance
 * @param assumed The assumed return value, if avoiding database hit is desired
 * @param session The session
 *
 * @return {@code true} if the given entity is transient (unsaved)
 */
public static CompletionStage<Boolean> isTransient(String entityName, Object entity, Boolean assumed,
												   SessionImplementor session) {
	if ( entity == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
		// an unfetched association can only point to
		// an entity that already exists in the db
		return CompletionStages.falseFuture();
	}

	// let the interceptor inspect the instance to decide
	Boolean isUnsaved = session.getInterceptor().isTransient( entity );
	if ( isUnsaved != null ) {
		return CompletionStages.completedFuture( isUnsaved );
	}

	// let the persister inspect the instance to decide
	final EntityPersister persister = session.getEntityPersister( entityName, entity );
	isUnsaved = persister.isTransient( entity, session );
	if ( isUnsaved != null ) {
		return CompletionStages.completedFuture( isUnsaved );
	}

	// we use the assumed value, if there is one, to avoid hitting
	// the database
	if ( assumed != null ) {
		return CompletionStages.completedFuture( assumed );
	}

	// hit the database, after checking the session cache for a snapshot
	ReactivePersistenceContextAdapter persistenceContext =
			(ReactivePersistenceContextAdapter) session.getPersistenceContextInternal();
	Serializable id = persister.getIdentifier(entity, session);
	return persistenceContext.reactiveGetDatabaseSnapshot( id, persister).thenApply(Objects::isNull);
}
 
Example 2
Source File: DefaultLockEventListener.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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() );
}
 
Example 3
Source File: DefaultReactiveLockEventListener.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public CompletionStage<Void> reactiveOnLock(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();
	final PersistenceContext persistenceContext = source.getPersistenceContextInternal();
	Object entity = persistenceContext.unproxyAndReassociate( event.getObject() );
	//TODO: if object was an uninitialized proxy, this is inefficient,
	//      resulting in two SQL selects

	EntityEntry entry = persistenceContext.getEntry(entity);
	CompletionStage<EntityEntry> stage;
	if (entry==null) {
		final EntityPersister persister = source.getEntityPersister( event.getEntityName(), entity );
		final Serializable id = persister.getIdentifier( entity, source );
		stage = ForeignKeys.isNotTransient( event.getEntityName(), entity, Boolean.FALSE, source )
				.thenApply(
						trans -> {
							if (!trans) {
								throw new TransientObjectException(
										"cannot lock an unsaved transient instance: " +
												persister.getEntityName()
								);
							}

							EntityEntry e = reassociate(event, entity, id, persister);
							cascadeOnLock(event, persister, entity);
							return e;
						} );

	}
	else {
		stage = CompletionStages.completedFuture(entry);
	}

	return stage.thenCompose( e -> upgradeLock( entity, e, event.getLockOptions(), event.getSession() ) );
}