FileDocCategorySizeDatePackage
ForeignKeys.javaAPI DocHibernate 3.2.57912Mon Jul 24 05:46:08 BST 2006org.hibernate.engine

ForeignKeys

public final class ForeignKeys extends Object
Algorithms related to foreign key constraint transparency
author
Gavin King

Fields Summary
Constructors Summary
private ForeignKeys()

Methods Summary
public static java.io.SerializablegetEntityIdentifierIfNotUnsaved(java.lang.String entityName, java.lang.Object object, SessionImplementor session)
Return the identifier of the persistent or transient object, or throw an exception if the instance is "unsaved" 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).

		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.getEntityMode() );
			}
			return id;
		}
	
public static booleanisNotTransient(java.lang.String entityName, java.lang.Object entity, java.lang.Boolean assumed, SessionImplementor session)
Is this instance persistent or detached? If assumed 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.

		if (entity instanceof HibernateProxy) return true;
		if ( session.getPersistenceContext().isEntryFor(entity) ) return true;
		return !isTransient(entityName, entity, assumed, session);
	
public static booleanisTransient(java.lang.String entityName, java.lang.Object entity, java.lang.Boolean assumed, SessionImplementor session)
Is this instance, which we know is not persistent, actually transient? If assumed 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.

		
		if (entity==LazyPropertyInitializer.UNFETCHED_PROPERTY) {
			// an unfetched association can only point to
			// an entity that already exists in the db
			return false;
		}
		
		// let the interceptor inspect the instance to decide
		Boolean isUnsaved = session.getInterceptor().isTransient(entity);
		if (isUnsaved!=null) return isUnsaved.booleanValue();
		
		// let the persister inspect the instance to decide
		EntityPersister persister = session.getEntityPersister(entityName, entity);
		isUnsaved = persister.isTransient(entity, session);
		if (isUnsaved!=null) return isUnsaved.booleanValue();

		// we use the assumed value, if there is one, to avoid hitting
		// the database
		if (assumed!=null) return assumed.booleanValue();
		
		// hit the database, after checking the session cache for a snapshot
		Object[] snapshot = session.getPersistenceContext()
		        .getDatabaseSnapshot( persister.getIdentifier( entity, session.getEntityMode() ), persister );
		return snapshot==null;