Java Code Examples for org.hibernate.engine.spi.SharedSessionContractImplementor#getContextEntityIdentifier()

The following examples show how to use org.hibernate.engine.spi.SharedSessionContractImplementor#getContextEntityIdentifier() . 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 lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the identifier of the persistent or transient object, or throw
 * an exception if the instance is "unsaved"
 * <p/>
 * Used by OneToOneType and ManyToOneType to determine what id value should
 * be used for an object that may or may not be associated with the session.
 * This does a "best guess" using any/all info available to use (not just the
 * EntityEntry).
 *
 * @param entityName The name of the entity
 * @param object The entity instance
 * @param session The session
 *
 * @return The identifier
 *
 * @throws TransientObjectException if the entity is transient (does not yet have an identifier)
 */
public static Serializable getEntityIdentifierIfNotUnsaved(
		final String entityName,
		final Object object,
		final SharedSessionContractImplementor session) throws TransientObjectException {
	if ( object == null ) {
		return null;
	}
	else {
		Serializable id = session.getContextEntityIdentifier( object );
		if ( id == null ) {
			// context-entity-identifier returns null explicitly if the entity
			// is not associated with the persistence context; so make some
			// deeper checks...
			if ( isTransient( entityName, object, Boolean.FALSE, session ) ) {
				throw new TransientObjectException(
						"object references an unsaved transient instance - save the transient instance before flushing: " +
								(entityName == null ? session.guessEntityName( object ) : entityName)
				);
			}
			id = session.getEntityPersister( entityName, object ).getIdentifier( object, session );
		}
		return id;
	}
}
 
Example 2
Source File: OneToOneType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean isNull(Object owner, SharedSessionContractImplementor session) {
	if ( propertyName != null ) {
		final EntityPersister ownerPersister = session.getFactory().getMetamodel().entityPersister( entityName );
		final Serializable id = session.getContextEntityIdentifier( owner );
		final EntityKey entityKey = session.generateEntityKey( id, ownerPersister );
		return session.getPersistenceContext().isPropertyNull( entityKey, getPropertyName() );
	}
	else {
		return false;
	}
}
 
Example 3
Source File: OneToOneType.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object hydrate(
	ResultSet rs,
	String[] names,
	SharedSessionContractImplementor session,
	Object owner) throws HibernateException, SQLException {
	return session.getContextEntityIdentifier(owner);
}
 
Example 4
Source File: EntityType.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Object replace(
		Object original,
		Object target,
		SharedSessionContractImplementor session,
		Object owner,
		Map copyCache) throws HibernateException {
	if ( original == null ) {
		return null;
	}
	Object cached = copyCache.get( original );
	if ( cached != null ) {
		return cached;
	}
	else {
		if ( original == target ) {
			return target;
		}
		if ( session.getContextEntityIdentifier( original ) == null &&
				ForeignKeys.isTransient( associatedEntityName, original, Boolean.FALSE, session ) ) {
			// original is transient; it is possible that original is a "managed" entity that has
			// not been made persistent yet, so check if copyCache contains original as a "managed" value
			// that corresponds with some "merge" value.
			if ( copyCache.containsValue( original ) ) {
				return original;
			}
			else {
				// the transient entity is not "managed"; add the merge/managed pair to copyCache
				final Object copy = session.getEntityPersister( associatedEntityName, original )
						.instantiate( null, session );
				copyCache.put( original, copy );
				return copy;
			}
		}
		else {
			Object id = getIdentifier( original, session );
			if ( id == null ) {
				throw new AssertionFailure(
						"non-transient entity has a null id: " + original.getClass()
								.getName()
				);
			}
			id = getIdentifierOrUniqueKeyType( session.getFactory() )
					.replace( id, null, session, owner, copyCache );
			return resolve( id, session, owner );
		}
	}
}