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

The following examples show how to use org.hibernate.engine.spi.SessionImplementor#generateEntityKey() . 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: ReactivePersistenceContextAdapter.java    From hibernate-reactive with GNU Lesser General Public License v2.1 6 votes vote down vote up
public CompletionStage<Object[]> reactiveGetDatabaseSnapshot(Serializable id, EntityPersister persister)
		throws HibernateException {

	SessionImplementor session = (SessionImplementor) getSession();
	final EntityKey key = session.generateEntityKey( id, persister );
	final Object[] cached = entitySnapshotsByKey == null ? null : entitySnapshotsByKey.get( key );
	if ( cached != null ) {
		return CompletionStages.completedFuture( cached == NO_ROW ? null : cached );
	}
	else {
		return ((ReactiveEntityPersister) persister).reactiveGetDatabaseSnapshot( id, session )
				.thenApply( snapshot -> {
					if ( entitySnapshotsByKey == null ) {
						entitySnapshotsByKey = new HashMap<>(8);
					}
					entitySnapshotsByKey.put( key, snapshot == null ? NO_ROW : snapshot );
					return snapshot;
				} );
	}
}
 
Example 2
Source File: DefaultFlushEntityEventListener.java    From lams with GNU General Public License v2.0 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().getStatistics()
						.optimisticFailure( persister.getEntityName() );
			}
			throw new StaleObjectStateException( persister.getEntityName(), id );
		}
		return snapshot;
	}
	// TODO: optimize away this lookup for entities w/o unsaved-value="undefined"
	final EntityKey entityKey = session.generateEntityKey( id, persister );
	return session.getPersistenceContext().getCachedDatabaseSnapshot( entityKey );
}
 
Example 3
Source File: Collections.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session) {
	final PersistenceContext persistenceContext = session.getPersistenceContext();
	final CollectionEntry entry = persistenceContext.getCollectionEntry( coll );
	final CollectionPersister loadedPersister = entry.getLoadedPersister();

	if ( loadedPersister != null && LOG.isDebugEnabled() ) {
		LOG.debugf(
				"Collection dereferenced: %s",
				MessageHelper.collectionInfoString( loadedPersister, 
						coll, entry.getLoadedKey(), session
				)
		);
	}

	// do a check
	final boolean hasOrphanDelete = loadedPersister != null && loadedPersister.hasOrphanDelete();
	if ( hasOrphanDelete ) {
		Serializable ownerId = loadedPersister.getOwnerEntityPersister().getIdentifier( coll.getOwner(), session );
		if ( ownerId == null ) {
			// the owning entity may have been deleted and its identifier unset due to
			// identifier-rollback; in which case, try to look up its identifier from
			// the persistence context
			if ( session.getFactory().getSessionFactoryOptions().isIdentifierRollbackEnabled() ) {
				final EntityEntry ownerEntry = persistenceContext.getEntry( coll.getOwner() );
				if ( ownerEntry != null ) {
					ownerId = ownerEntry.getId();
				}
			}
			if ( ownerId == null ) {
				throw new AssertionFailure( "Unable to determine collection owner identifier for orphan-delete processing" );
			}
		}
		final EntityKey key = session.generateEntityKey( ownerId, loadedPersister.getOwnerEntityPersister() );
		final Object owner = persistenceContext.getEntity( key );
		if ( owner == null ) {
			throw new AssertionFailure(
					"collection owner not associated with session: " +
					loadedPersister.getRole()
			);
		}
		final EntityEntry e = persistenceContext.getEntry( owner );
		//only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
		if ( e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE ) {
			throw new HibernateException(
					"A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " +
					loadedPersister.getRole()
			);
		}
	}

	// do the work
	entry.setCurrentPersister( null );
	entry.setCurrentKey( null );
	prepareCollectionForUpdate( coll, entry, session.getFactory() );

}