FileDocCategorySizeDatePackage
EntityType.javaAPI DocHibernate 3.2.517658Wed Nov 08 15:02:08 GMT 2006org.hibernate.type

EntityType

public abstract class EntityType extends AbstractType implements AssociationType
Base for types which map associations to persistent entities.
author
Gavin King

Fields Summary
private final String
associatedEntityName
protected final String
uniqueKeyPropertyName
protected final boolean
isEmbeddedInXML
private final boolean
eager
private final boolean
unwrapProxy
private transient Class
returnedClass
Constructors Summary
protected EntityType(String entityName, String uniqueKeyPropertyName, boolean eager, boolean isEmbeddedInXML, boolean unwrapProxy)
Constructs the requested entity type mapping.

param
entityName The name of the associated entity.
param
uniqueKeyPropertyName The property-ref name, or null if we reference the PK of the associated entity.
param
eager Is eager fetching enabled.
param
isEmbeddedInXML Should values of this mapping be embedded in XML modes?
param
unwrapProxy Is unwrapping of proxies allowed for this association; unwrapping says to return the "implementation target" of lazy prooxies; typically only possible with lazy="no-proxy".

		this.associatedEntityName = entityName;
		this.uniqueKeyPropertyName = uniqueKeyPropertyName;
		this.isEmbeddedInXML = isEmbeddedInXML;
		this.eager = eager;
		this.unwrapProxy = unwrapProxy;
	
Methods Summary
public intcompare(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)
{@inheritDoc}

		return 0; //TODO: entities CAN be compared, by PK, fix this! -> only if/when we can extract the id values....
	
public java.lang.ObjectdeepCopy(java.lang.Object value, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)
{@inheritDoc}

		return value; //special case ... this is the leaf of the containment graph, even though not immutable
	
private java.lang.ClassdetermineAssociatedEntityClass()

		try {
			return ReflectHelper.classForName( getAssociatedEntityName() );
		}
		catch ( ClassNotFoundException cnfe ) {
			return java.util.Map.class;
		}
	
public java.lang.ObjectfromXMLNode(org.dom4j.Node xml, org.hibernate.engine.Mapping factory)
{@inheritDoc}

		if ( !isEmbeddedInXML ) {
			return getIdentifierType(factory).fromXMLNode(xml, factory);
		}
		else {
			return xml;
		}
	
public final java.lang.StringgetAssociatedEntityName()
The name of the associated entity.

return
The associated entity name.

		return associatedEntityName;
	
public java.lang.StringgetAssociatedEntityName(org.hibernate.engine.SessionFactoryImplementor factory)
The name of the associated entity.

param
factory The session factory, for resolution.
return
The associated entity name.

		return getAssociatedEntityName();
	
public org.hibernate.persister.entity.JoinablegetAssociatedJoinable(org.hibernate.engine.SessionFactoryImplementor factory)
Retrieves the {@link Joinable} defining the associated entity.

param
factory The session factory.
return
The associated joinable
throws
MappingException Generally indicates an invalid entity name.

		return ( Joinable ) factory.getEntityPersister( associatedEntityName );
	
public intgetHashCode(java.lang.Object x, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)
{@inheritDoc}

		EntityPersister persister = factory.getEntityPersister(associatedEntityName);
		if ( !persister.canExtractIdOutOfEntity() ) {
			return super.getHashCode(x, entityMode);
		}

		final Serializable id;
		if (x instanceof HibernateProxy) {
			id = ( (HibernateProxy) x ).getHibernateLazyInitializer().getIdentifier();
		}
		else {
			id = persister.getIdentifier(x, entityMode);
		}
		return persister.getIdentifierType().getHashCode(id, entityMode, factory);
	
protected final java.lang.ObjectgetIdentifier(java.lang.Object value, org.hibernate.engine.SessionImplementor session)

		if ( isNotEmbedded(session) ) {
			return value;
		}

		if ( isReferenceToPrimaryKey() ) {
			return ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session ); //tolerates nulls
		}
		else if ( value == null ) {
			return null;
		}
		else {
			EntityPersister entityPersister = session.getFactory().getEntityPersister( getAssociatedEntityName() );
			Object propertyValue = entityPersister.getPropertyValue( value, uniqueKeyPropertyName, session.getEntityMode() );
			// We now have the value of the property-ref we reference.  However,
			// we need to dig a little deeper, as that property might also be
			// an entity type, in which case we need to resolve its identitifier
			Type type = entityPersister.getPropertyType( uniqueKeyPropertyName );
			if ( type.isEntityType() ) {
				propertyValue = ( ( EntityType ) type ).getIdentifier( propertyValue, session );
			}

			return propertyValue;
		}
	
private static java.io.SerializablegetIdentifier(java.lang.Object object, org.hibernate.persister.entity.EntityPersister persister, org.hibernate.EntityMode entityMode)
Get the identifier value of an instance or proxy.

Intended only for loggin purposes!!!

param
object The object from which to extract the identifier.
param
persister The entity persister
param
entityMode The entity mode
return
The extracted identifier.

		if (object instanceof HibernateProxy) {
			HibernateProxy proxy = (HibernateProxy) object;
			LazyInitializer li = proxy.getHibernateLazyInitializer();
			return li.getIdentifier();
		}
		else {
			return persister.getIdentifier( object, entityMode );
		}
	
public final java.lang.StringgetIdentifierOrUniqueKeyPropertyName(org.hibernate.engine.Mapping factory)
The name of the property on the associated entity to which our FK refers

param
factory The mappings...
return
The appropriate property name.
throws
MappingException Generally, if unable to resolve the associated entity name

		if ( isReferenceToPrimaryKey() ) {
			return factory.getIdentifierPropertyName( getAssociatedEntityName() );
		}
		else {
			return uniqueKeyPropertyName;
		}
	
public final TypegetIdentifierOrUniqueKeyType(org.hibernate.engine.Mapping factory)
Determine the type of either (1) the identifier if we reference the associated entity's PK or (2) the unique key to which we refer (i.e. the property-ref).

param
factory The mappings...
return
The appropriate type.
throws
MappingException Generally, if unable to resolve the associated entity name or unique key property name.

		if ( isReferenceToPrimaryKey() ) {
			return getIdentifierType(factory);
		}
		else {
			Type type = factory.getReferencedPropertyType( getAssociatedEntityName(), uniqueKeyPropertyName );
			if ( type.isEntityType() ) {
				type = ( ( EntityType ) type).getIdentifierOrUniqueKeyType( factory );
			}
			return type;
		}
	
TypegetIdentifierType(org.hibernate.engine.Mapping factory)
Convenience method to locate the identifier type of the associated entity.

param
factory The mappings...
return
The identifier type

		return factory.getIdentifierType( getAssociatedEntityName() );
	
TypegetIdentifierType(org.hibernate.engine.SessionImplementor session)
Convenience method to locate the identifier type of the associated entity.

param
session The originating session
return
The identifier type

		return getIdentifierType( session.getFactory() );
	
public java.lang.StringgetLHSPropertyName()

		return null;
	
public java.lang.StringgetName()
For entity types, the name correlates to the associated entity name.

		return associatedEntityName;
	
public java.lang.StringgetOnCondition(java.lang.String alias, org.hibernate.engine.SessionFactoryImplementor factory, java.util.Map enabledFilters)

		if ( isReferenceToPrimaryKey() ) { //TODO: this is a bit arbitrary, expose a switch to the user?
			return "";
		}
		else {
			return getAssociatedJoinable( factory ).filterFragment( alias, enabledFilters );
		}
	
public java.lang.StringgetPropertyName()

		return null;
	
public java.lang.StringgetRHSUniqueKeyPropertyName()

		return uniqueKeyPropertyName;
	
public final java.lang.ClassgetReturnedClass()
This returns the wrong class for an entity with a proxy, or for a named entity. Theoretically it should return the proxy class, but it doesn't.

The problem here is that we do not necessarily have a ref to the associated entity persister (nor to the session factory, to look it up) which is really needed to "do the right thing" here...

return
The entiyt class.

		if ( returnedClass == null ) {
			returnedClass = determineAssociatedEntityClass();
		}
		return returnedClass;
	
public TypegetSemiResolvedType(org.hibernate.engine.SessionFactoryImplementor factory)

		return factory.getEntityPersister( associatedEntityName ).getIdentifierType();
	
public booleanisAssociationType()
An entity type is a type of association type

return
True.

		return true;
	
public booleanisEmbeddedInXML()
{@inheritDoc}

		return isEmbeddedInXML;
	
public final booleanisEntityType()
Explicitly, an entity type is an entity type ;)

return
True.

		return true;
	
public booleanisEqual(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode, org.hibernate.engine.SessionFactoryImplementor factory)
{@inheritDoc}

		EntityPersister persister = factory.getEntityPersister(associatedEntityName);
		if ( !persister.canExtractIdOutOfEntity() ) {
			return super.isEqual(x, y, entityMode);
		}

		Serializable xid;
		if (x instanceof HibernateProxy) {
			xid = ( (HibernateProxy) x ).getHibernateLazyInitializer()
					.getIdentifier();
		}
		else {
			xid = persister.getIdentifier(x, entityMode);
		}

		Serializable yid;
		if (y instanceof HibernateProxy) {
			yid = ( (HibernateProxy) y ).getHibernateLazyInitializer()
					.getIdentifier();
		}
		else {
			yid = persister.getIdentifier(y, entityMode);
		}

		return persister.getIdentifierType()
				.isEqual(xid, yid, entityMode, factory);
	
public booleanisMutable()
{@inheritDoc}

		return false;
	
protected booleanisNotEmbedded(org.hibernate.engine.SessionImplementor session)

		return !isEmbeddedInXML && session.getEntityMode()==EntityMode.DOM4J;
	
protected booleanisNull(java.lang.Object owner, org.hibernate.engine.SessionImplementor session)

		return false;
	
protected abstract booleanisNullable()

public abstract booleanisOneToOne()

public booleanisReferenceToPrimaryKey()
Does this association foreign key reference the primary key of the other table? Otherwise, it references a property-ref.

return
True if this association reference the PK of the associated entity.

		return uniqueKeyPropertyName==null;
	
public final booleanisSame(java.lang.Object x, java.lang.Object y, org.hibernate.EntityMode entityMode)
Two entities are considered the same when their instances are the same.

param
x One entity instance
param
y Another entity instance
param
entityMode The entity mode.
return
True if x == y; false otherwise.

		return x == y;
	
public booleanisXMLElement()
{@inheritDoc}

		return isEmbeddedInXML;
	
public java.lang.ObjectloadByUniqueKey(java.lang.String entityName, java.lang.String uniqueKeyPropertyName, java.lang.Object key, org.hibernate.engine.SessionImplementor session)
Load an instance by a unique key that is not the primary key.

param
entityName The name of the entity to load
param
uniqueKeyPropertyName The name of the property defining the uniqie key.
param
key The unique key property value.
param
session The originating session.
return
The loaded entity
throws
HibernateException generally indicates problems performing the load.

		final SessionFactoryImplementor factory = session.getFactory();
		UniqueKeyLoadable persister = ( UniqueKeyLoadable ) factory.getEntityPersister( entityName );

		//TODO: implement caching?! proxies?!

		EntityUniqueKey euk = new EntityUniqueKey(
				entityName, 
				uniqueKeyPropertyName, 
				key, 
				getIdentifierOrUniqueKeyType( factory ),
				session.getEntityMode(), 
				session.getFactory()
		);

		final PersistenceContext persistenceContext = session.getPersistenceContext();
		Object result = persistenceContext.getEntity( euk );
		if ( result == null ) {
			result = persister.loadByUniqueKey( uniqueKeyPropertyName, key, session );
		}
		return result == null ? null : persistenceContext.proxyFor( result );
	
public java.lang.ObjectnullSafeGet(java.sql.ResultSet rs, java.lang.String name, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)
{@inheritDoc}

		return nullSafeGet( rs, new String[] {name}, session, owner );
	
public final java.lang.ObjectnullSafeGet(java.sql.ResultSet rs, java.lang.String[] names, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)
{@inheritDoc}

		return resolve( hydrate(rs, names, session, owner), session, owner );
	
public java.lang.Objectreplace(java.lang.Object original, java.lang.Object target, org.hibernate.engine.SessionImplementor session, java.lang.Object owner, java.util.Map copyCache)
{@inheritDoc}

		if ( original == null ) {
			return null;
		}
		Object cached = copyCache.get(original);
		if ( cached != null ) {
			return cached;
		}
		else {
			if ( original == target ) {
				return target;
			}
			Object id = getIdentifier( original, session );
			if ( id == null ) {
				throw new AssertionFailure("cannot copy a reference to an object with a null id");
			}
			id = getIdentifierOrUniqueKeyType( session.getFactory() )
					.replace(id, null, session, owner, copyCache);
			return resolve( id, session, owner );
		}
	
public java.lang.Objectresolve(java.lang.Object value, org.hibernate.engine.SessionImplementor session, java.lang.Object owner)
Resolve an identifier or unique key value

		if ( isNotEmbedded( session ) ) {
			return value;
		}

		if ( value == null ) {
			return null;
		}
		else {
			if ( isNull( owner, session ) ) {
				return null; //EARLY EXIT!
			}

			if ( isReferenceToPrimaryKey() ) {
				return resolveIdentifier( (Serializable) value, session );
			}
			else {
				return loadByUniqueKey( getAssociatedEntityName(), uniqueKeyPropertyName, value, session );
			}
		}
	
protected final java.lang.ObjectresolveIdentifier(java.io.Serializable id, org.hibernate.engine.SessionImplementor session)
Resolve an identifier via a load.

param
id The entity id to resolve
param
session The orginating session.
return
The resolved identifier (i.e., loaded entity).
throws
org.hibernate.HibernateException Indicates problems performing the load.

		boolean isProxyUnwrapEnabled = unwrapProxy &&
				session.getFactory()
						.getEntityPersister( getAssociatedEntityName() )
						.isInstrumented( session.getEntityMode() );

		Object proxyOrEntity = session.internalLoad(
				getAssociatedEntityName(),
				id,
				eager,
				isNullable() && !isProxyUnwrapEnabled
		);

		if ( proxyOrEntity instanceof HibernateProxy ) {
			( ( HibernateProxy ) proxyOrEntity ).getHibernateLazyInitializer()
					.setUnwrap( isProxyUnwrapEnabled );
		}

		return proxyOrEntity;
	
public voidsetToXMLNode(org.dom4j.Node node, java.lang.Object value, org.hibernate.engine.SessionFactoryImplementor factory)
{@inheritDoc}

		if ( !isEmbeddedInXML ) {
			getIdentifierType(factory).setToXMLNode(node, value, factory);
		}
		else {
			Element elt = (Element) value;
			replaceNode( node, new ElementWrapper(elt) );
		}
	
public java.lang.StringtoLoggableString(java.lang.Object value, org.hibernate.engine.SessionFactoryImplementor factory)
Generate a loggable representation of an instance of the value mapped by this type.

param
value The instance to be logged.
param
factory The session factory.
return
The loggable string.
throws
HibernateException Generally some form of resolution problem.

		if ( value == null ) {
			return "null";
		}
		
		EntityPersister persister = factory.getEntityPersister( associatedEntityName );
		StringBuffer result = new StringBuffer().append( associatedEntityName );

		if ( persister.hasIdentifierProperty() ) {
			final EntityMode entityMode = persister.guessEntityMode( value );
			final Serializable id;
			if ( entityMode == null ) {
				if ( isEmbeddedInXML ) {
					throw new ClassCastException( value.getClass().getName() );
				}
				id = ( Serializable ) value;
			}
			else {
				id = getIdentifier( value, persister, entityMode );
			}
			
			result.append( '#" )
				.append( persister.getIdentifierType().toLoggableString( id, factory ) );
		}
		
		return result.toString();
	
public java.lang.StringtoString()
Generates a string representation of this type.

return
string rep

		return getClass().getName() + '(" + getAssociatedEntityName() + ')";