Code example for Mapping

Methods: getIdentifierTypegetReferencedPropertyType

0
	 * 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,  
			String uniqueKeyPropertyName,  
			Object key,  
			SessionImplementor session) throws HibernateException { 
		final SessionFactoryImplementor factory = session.getFactory(); 
		UniqueKeyLoadable persister = ( UniqueKeyLoadable ) factory.getEntityPersister( entityName ); 
 
		//TODO: implement caching?! proxies?! 
 
		EntityUniqueKey euk = new EntityUniqueKey( 
				entityName,  
				uniqueKeyPropertyName,  
				key,  
				getIdentifierOrUniqueKeyType( factory ), 
				persister.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 ); 
	} 
 
}