Code example for Mapping

Methods: getIdentifierType

0
 
	protected final Object getIdentifier(Object value, SessionImplementor session) throws HibernateException { 
		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 ); 
			// 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; 
		} 
	} 
 
	protected boolean isNotEmbedded(SessionImplementor session) { 
//		return !isEmbeddedInXML; 
		return false; 
	} 
 
	/** 
	 * 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. 
	 */ 
	public String toLoggableString(Object value, SessionFactoryImplementor factory) { 
		if ( value == null ) { 
			return "null"; 
		} 
		 
		EntityPersister persister = factory.getEntityPersister( associatedEntityName ); 
		StringBuffer result = new StringBuffer().append( associatedEntityName ); 
 
		if ( persister.hasIdentifierProperty() ) { 
			final EntityMode entityMode = persister.getEntityMode(); 
			final Serializable id; 
			if ( entityMode == null ) { 
				if ( isEmbeddedInXML ) { 
					throw new ClassCastException( value.getClass().getName() ); 
				} 
				id = ( Serializable ) value; 
			} 
			else { 
				id = persister.getIdentifier( value ); 
			} 
			 
			result.append( '#' ) 
				.append( persister.getIdentifierType().toLoggableString( id, factory ) ); 
		} 
		 
		return result.toString(); 
	} 
 
	/** 
	 * Is the association modeled here defined as a 1-1 in the database (physical model)? 
	 * 
	 * @return True if a 1-1 in the database; false otherwise. 
	 */ 
	public abstract boolean isOneToOne(); 
 
	/** 
	 * Is the association modeled here a 1-1 according to the logical moidel? 
	 * 
	 * @return True if a 1-1 in the logical model; false otherwise. 
	 */ 
	public boolean isLogicalOneToOne() { 
		return isOneToOne(); 
	} 
 
	/** 
	 * Convenience method to locate the identifier type of the associated entity. 
	 * 
	 * @param factory The mappings... 
	 * @return The identifier type 
	 */ 
	Type getIdentifierType(Mapping factory) { 
		return factory.getIdentifierType( getAssociatedEntityName() ); 
	} 
 
	/** 
	 * Convenience method to locate the identifier type of the associated entity. 
	 * 
	 * @param session The originating session 
	 * @return The identifier type 
	 */ 
	Type getIdentifierType(SessionImplementor session) { 
		return getIdentifierType( session.getFactory() ); 
	} 
 
	/** 
	 * 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. 
	 */ 
	public final Type getIdentifierOrUniqueKeyType(Mapping factory) throws MappingException { 
		if ( isReferenceToPrimaryKey() ) { 
			return getIdentifierType(factory); 
		} 
		else { 
			Type type = factory.getReferencedPropertyType( getAssociatedEntityName(), uniqueKeyPropertyName ); 
			if ( type.isEntityType() ) { 
				type = ( ( EntityType ) type).getIdentifierOrUniqueKeyType( factory ); 
			} 
			return type; 
		} 
	} 
 
	/** 
	 * 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 
	 */ 
	public final String getIdentifierOrUniqueKeyPropertyName(Mapping factory) 
	throws MappingException { 
		if ( isReferenceToPrimaryKey() ) { 
			return factory.getIdentifierPropertyName( getAssociatedEntityName() ); 
		} 
		else { 
			return uniqueKeyPropertyName; 
		} 
	} 
	 
	protected abstract boolean isNullable(); 
 
	/** 
	 * 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. 
	 */ 
	protected final Object resolveIdentifier(Serializable id, SessionImplementor session) throws HibernateException { 
		boolean isProxyUnwrapEnabled = unwrapProxy && 
				session.getFactory() 
						.getEntityPersister( getAssociatedEntityName() ) 
						.isInstrumented(); 
 
		Object proxyOrEntity = session.internalLoad( 
				getAssociatedEntityName(), 
				id, 
				eager, 
				isNullable() && !isProxyUnwrapEnabled 
		); 
 
		if ( proxyOrEntity instanceof HibernateProxy ) { 
			( ( HibernateProxy ) proxyOrEntity ).getHibernateLazyInitializer() 
					.setUnwrap( isProxyUnwrapEnabled ); 
		} 
 
		return proxyOrEntity; 
	} 
 
	protected boolean isNull(Object owner, SessionImplementor session) { 
		return false; 
	} 
 
	/** 
	 * 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. 
	 */ 
	public Object loadByUniqueKey( 
			String entityName,  
Contextual code suggestions in your IDE  Get Codota for Java