Java Code Examples for org.hibernate.pretty.MessageHelper#infoString()

The following examples show how to use org.hibernate.pretty.MessageHelper#infoString() . 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: EntityVerifyVersionProcess.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
	final EntityPersister persister = entry.getPersister();

	if ( !entry.isExistsInDatabase() ) {
		// HHH-9419: We cannot check for a version of an entry we ourselves deleted
		return;
	}

	final Object latestVersion = persister.getCurrentVersion( entry.getId(), session );
	if ( !entry.getVersion().equals( latestVersion ) ) {
		throw new OptimisticLockException(
				object,
				"Newer version [" + latestVersion +
						"] of entity [" + MessageHelper.infoString( entry.getEntityName(), entry.getId() ) +
						"] found in database"
		);
	}
}
 
Example 2
Source File: UnresolvedEntityInsertActions.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void logCannotResolveNonNullableTransientDependencies(SharedSessionContractImplementor session) {
	for ( Map.Entry<Object,Set<AbstractEntityInsertAction>> entry : dependentActionsByTransientEntity.entrySet() ) {
		final Object transientEntity = entry.getKey();
		final String transientEntityName = session.guessEntityName( transientEntity );
		final Serializable transientEntityId = session.getFactory().getMetamodel().entityPersister( transientEntityName ).getIdentifier( transientEntity, session );
		final String transientEntityString = MessageHelper.infoString( transientEntityName, transientEntityId );
		final Set<String> dependentEntityStrings = new TreeSet<>();
		final Set<String> nonNullableTransientPropertyPaths = new TreeSet<>();
		for ( AbstractEntityInsertAction dependentAction : entry.getValue() ) {
			dependentEntityStrings.add( MessageHelper.infoString( dependentAction.getEntityName(), dependentAction.getId() ) );
			for ( String path : dependenciesByAction.get( dependentAction ).getNonNullableTransientPropertyPaths( transientEntity ) ) {
				final String fullPath = dependentAction.getEntityName() + '.' + path;
				nonNullableTransientPropertyPaths.add( fullPath );
			}
		}

		LOG.cannotResolveNonNullableTransientDependencies(
				transientEntityString,
				dependentEntityStrings,
				nonNullableTransientPropertyPaths
		);
	}
}
 
Example 3
Source File: EntityCopyNotAllowedObserver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void entityCopyDetected(
		Object managedEntity,
		Object mergeEntity1,
		Object mergeEntity2,
		EventSource session) {
	if ( mergeEntity1 == managedEntity && mergeEntity2 == managedEntity) {
		throw new AssertionFailure( "entity1 and entity2 are the same as managedEntity; must be different." );
	}
	final String managedEntityString = 	MessageHelper.infoString(
			session.getEntityName( managedEntity ),
			session.getIdentifier( managedEntity )
	);
	throw new IllegalStateException(
			"Multiple representations of the same entity " + managedEntityString + " are being merged. " +
					getManagedOrDetachedEntityString( managedEntity, mergeEntity1 ) + "; " +
					getManagedOrDetachedEntityString( managedEntity, mergeEntity2 )
	);
}
 
Example 4
Source File: StatefulPersistenceContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object[] getCachedDatabaseSnapshot(EntityKey key) {
	final Object snapshot = entitySnapshotsByKey.get( key );
	if ( snapshot == NO_ROW ) {
		throw new IllegalStateException(
				"persistence context reported no row snapshot for "
						+ MessageHelper.infoString( key.getEntityName(), key.getIdentifier() )
		);
	}
	return (Object[]) snapshot;
}
 
Example 5
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 6
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 7
Source File: EntityUniqueKey.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String toString() {
	return "EntityUniqueKey" + MessageHelper.infoString( entityName, uniqueKeyName, key );
}
 
Example 8
Source File: EntityKey.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String toString() {
	return "EntityKey" +
			MessageHelper.infoString( this.persister, identifier, persister.getFactory() );
}
 
Example 9
Source File: AbstractEntityEntry.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public String toString() {
	return "EntityEntry" +
			MessageHelper.infoString( getPersister().getEntityName(), id ) +
			'(' + getStatus() + ')';
}
 
Example 10
Source File: Cascade.java    From hibernate-reactive with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void cascadeLogicalOneToOneOrphanRemoval(
			final int componentPathStackDepth,
			final Object child,
			final Type type,
			final CascadeStyle style,
			final String propertyName,
			final boolean isCascadeDeleteEnabled) throws HibernateException {

		// potentially we need to handle orphan deletes for one-to-ones here...
		if ( isLogicalOneToOne( type ) ) {
			// We have a physical or logical one-to-one.  See if the attribute cascade settings and action-type require
			// orphan checking
			if ( style.hasOrphanDelete() && action.deleteOrphans() ) {
				// value is orphaned if loaded state for this property shows not null
				// because it is currently null.
				final PersistenceContext persistenceContext = eventSource.getPersistenceContextInternal();
				final EntityEntry entry = persistenceContext.getEntry( parent );
				if ( entry != null && entry.getStatus() != Status.SAVING ) {
					Object loadedValue;
					if ( componentPathStackDepth == 0 ) {
						// association defined on entity
						loadedValue = entry.getLoadedValue( propertyName );
					}
					else {
						// association defined on component
						// 		todo : this is currently unsupported because of the fact that
						//		we do not know the loaded state of this value properly
						//		and doing so would be very difficult given how components and
						//		entities are loaded (and how 'loaded state' is put into the
						//		EntityEntry).  Solutions here are to either:
						//			1) properly account for components as a 2-phase load construct
						//			2) just assume the association was just now orphaned and
						// 				issue the orphan delete.  This would require a special
						//				set of SQL statements though since we do not know the
						//				orphaned value, something a delete with a subquery to
						// 				match the owner.
//							final EntityType entityType = (EntityType) type;
//							final String getPropertyPath = composePropertyPath( entityType.getPropertyName() );
						loadedValue = null;
					}

					// orphaned if the association was nulled (child == null) or receives a new value while the
					// entity is managed (without first nulling and manually flushing).
					if ( child == null || ( loadedValue != null && child != loadedValue ) ) {
						EntityEntry valueEntry = persistenceContext.getEntry( loadedValue );

						if ( valueEntry == null && loadedValue instanceof HibernateProxy ) {
							// un-proxy and re-associate for cascade operation
							// useful for @OneToOne defined as FetchType.LAZY
							loadedValue = persistenceContext.unproxyAndReassociate( loadedValue );
							valueEntry = persistenceContext.getEntry( loadedValue );

							// HHH-11965
							// Should the unwrapped proxy value be equal via reference to the entity's property value
							// provided by the 'child' variable, we should not trigger the orphan removal of the
							// associated one-to-one.
							if ( child == loadedValue ) {
								// do nothing
								return;
							}
						}

						if ( valueEntry != null ) {
							final String entityName = valueEntry.getPersister().getEntityName();
							if ( LOG.isTraceEnabled() ) {
								final Serializable id = valueEntry.getPersister().getIdentifier( loadedValue, eventSource );
								final String description = MessageHelper.infoString( entityName, id );
								LOG.tracev( "Deleting orphaned entity instance: {0}", description );
							}

							if ( type.isAssociationType() && ( (AssociationType) type ).getForeignKeyDirection().equals(
									ForeignKeyDirection.TO_PARENT
							) ) {
								// If FK direction is to-parent, we must remove the orphan *before* the queued update(s)
								// occur.  Otherwise, replacing the association on a managed entity, without manually
								// nulling and flushing, causes FK constraint violations.
								eventSource.removeOrphanBeforeUpdates( entityName, loadedValue );
							}
							else {
								// Else, we must delete after the updates.
								eventSource.delete( entityName, loadedValue, isCascadeDeleteEnabled, new HashSet<>() );
							}
						}
					}
				}
			}
		}
	}
 
Example 11
Source File: DefaultSaveOrUpdateEventListener.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
	log.trace( "ignoring persistent instance" );

	EntityEntry entityEntry = event.getEntry();
	if ( entityEntry == null ) {
		throw new AssertionFailure( "entity was transient or detached" );
	}
	else {

		if ( entityEntry.getStatus() == Status.DELETED ) {
			throw new AssertionFailure( "entity was deleted" );
		}

		final SessionFactoryImplementor factory = event.getSession().getFactory();

		Serializable requestedId = event.getRequestedId();

		Serializable savedId;
		if ( requestedId == null ) {
			savedId = entityEntry.getId();
		}
		else {

			final boolean isEqual = !entityEntry.getPersister().getIdentifierType()
					.isEqual( requestedId, entityEntry.getId(), event.getSession().getEntityMode(), factory );

			if ( isEqual ) {
				throw new PersistentObjectException(
						"object passed to save() was already persistent: " +
								MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory )
				);
			}

			savedId = requestedId;

		}

		if ( log.isTraceEnabled() ) {
			log.trace(
					"object already associated with session: " +
							MessageHelper.infoString( entityEntry.getPersister(), savedId, factory )
			);
		}

		return savedId;

	}
}
 
Example 12
Source File: EntityKey.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String toString() {
	return "EntityKey" + 
		MessageHelper.infoString( factory.getEntityPersister( entityName ), identifier, factory );
}
 
Example 13
Source File: DefaultSaveOrUpdateEventListener.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected Serializable entityIsPersistent(SaveOrUpdateEvent event) throws HibernateException {
	final boolean traceEnabled = LOG.isTraceEnabled();
	if ( traceEnabled ) {
		LOG.trace( "Ignoring persistent instance" );
	}
	EntityEntry entityEntry = event.getEntry();
	if ( entityEntry == null ) {
		throw new AssertionFailure( "entity was transient or detached" );
	}
	else {

		if ( entityEntry.getStatus() == Status.DELETED ) {
			throw new AssertionFailure( "entity was deleted" );
		}

		final SessionFactoryImplementor factory = event.getSession().getFactory();

		Serializable requestedId = event.getRequestedId();

		Serializable savedId;
		if ( requestedId == null ) {
			savedId = entityEntry.getId();
		}
		else {

			final boolean isEqual = !entityEntry.getPersister().getIdentifierType()
					.isEqual( requestedId, entityEntry.getId(), factory );

			if ( isEqual ) {
				throw new PersistentObjectException(
						"object passed to save() was already persistent: " +
								MessageHelper.infoString( entityEntry.getPersister(), requestedId, factory )
				);
			}

			savedId = requestedId;

		}

		if ( traceEnabled ) {
			LOG.tracev(
					"Object already associated with session: {0}",
					MessageHelper.infoString( entityEntry.getPersister(), savedId, factory )
			);
		}

		return savedId;

	}
}
 
Example 14
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void processGeneratedProperties(
			Serializable id,
			Object entity,
			Object[] state,
			SharedSessionContractImplementor session,
			String selectionSQL,
			GenerationTiming matchTiming) {
		// force immediate execution of the insert batch (if one)
		session.getJdbcCoordinator().executeBatch();

		try {
			PreparedStatement ps = session
					.getJdbcCoordinator()
					.getStatementPreparer()
					.prepareStatement( selectionSQL );
			try {
				getIdentifierType().nullSafeSet( ps, id, 1, session );
				ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract( ps );
				try {
					if ( !rs.next() ) {
						throw new HibernateException(
								"Unable to locate row for retrieval of generated properties: " +
										MessageHelper.infoString( this, id, getFactory() )
						);
					}
					int propertyIndex = -1;
					for ( NonIdentifierAttribute attribute : entityMetamodel.getProperties() ) {
						propertyIndex++;
						if ( isValueGenerationRequired( attribute, matchTiming ) ) {
							final Object hydratedState = attribute.getType().hydrate(
									rs, getPropertyAliases(
											"",
											propertyIndex
									), session, entity
							);
							state[propertyIndex] = attribute.getType().resolve( hydratedState, session, entity );
							setPropertyValue( entity, propertyIndex, state[propertyIndex] );
						}
					}

//					for ( int i = 0; i < getPropertySpan(); i++ ) {
//						if ( includeds[i] != ValueInclusion.NONE ) {
//							Object hydratedState = getPropertyTypes()[i].hydrate( rs, getPropertyAliases( "", i ), session, entity );
//							state[i] = getPropertyTypes()[i].resolve( hydratedState, session, entity );
//							setPropertyValue( entity, i, state[i] );
//						}
//					}
				}
				finally {
					if ( rs != null ) {
						session.getJdbcCoordinator().getResourceRegistry().release( rs, ps );
					}
				}
			}
			finally {
				session.getJdbcCoordinator().getResourceRegistry().release( ps );
				session.getJdbcCoordinator().afterStatementExecution();
			}
		}
		catch (SQLException e) {
			throw getFactory().getSQLExceptionHelper().convert(
					e,
					"unable to select generated column values",
					selectionSQL
			);
		}

	}
 
Example 15
Source File: StaleObjectStateException.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public String getMessage() {
	return super.getMessage() + " : " + MessageHelper.infoString( entityName, identifier );
}
 
Example 16
Source File: EntityEntry.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String toString() {
	return "EntityEntry" + 
			MessageHelper.infoString(entityName, id) + 
			'(' + status + ')';
}
 
Example 17
Source File: EntityUniqueKey.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String toString() {
	return "EntityUniqueKey" + MessageHelper.infoString(entityName, uniqueKeyName, key);
}
 
Example 18
Source File: UnresolvableObjectException.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String getMessage() {
	return super.getMessage() + ": " +
		MessageHelper.infoString(entityName, identifier);
}
 
Example 19
Source File: NonUniqueObjectException.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String getMessage() {
	return super.getMessage() + ": " +
		MessageHelper.infoString(entityName, identifier);
}
 
Example 20
Source File: StatefulPersistenceContext.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Retrieve the cached database snapshot for the requested entity key.
 * <p/>
 * This differs from {@link #getDatabaseSnapshot} is two important respects:<ol>
 * <li>no snapshot is obtained from the database if not already cached</li>
 * <li>an entry of {@link #NO_ROW} here is interpretet as an exception</li>
 * </ol>
 * @param key The entity key for which to retrieve the cached snapshot
 * @return The cached snapshot
 * @throws IllegalStateException if the cached snapshot was == {@link #NO_ROW}.
 */
public Object[] getCachedDatabaseSnapshot(EntityKey key) {
	Object snapshot = entitySnapshotsByKey.get( key );
	if ( snapshot == NO_ROW ) {
		throw new IllegalStateException( "persistence context reported no row snapshot for " + MessageHelper.infoString( key.getEntityName(), key.getIdentifier() ) );
	}
	return ( Object[] ) snapshot;
}