Java Code Examples for org.hibernate.engine.spi.SessionFactoryImplementor.getEntityPersister()

The following are Jave code examples for showing how to use getEntityPersister() of the org.hibernate.engine.spi.SessionFactoryImplementor class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: AbstractEmptinessExpression.java   View Source Code Vote up 6 votes
protected QueryableCollection getQueryableCollection(
		String entityName,
		String propertyName,
		SessionFactoryImplementor factory) throws HibernateException {
	final PropertyMapping ownerMapping = (PropertyMapping) factory.getEntityPersister( entityName );
	final Type type = ownerMapping.toType( propertyName );
	if ( !type.isCollectionType() ) {
		throw new MappingException(
				"Property path [" + entityName + "." + propertyName + "] does not reference a collection"
		);
	}

	final String role = ( (CollectionType) type ).getRole();
	try {
		return (QueryableCollection) factory.getCollectionPersister( role );
	}
	catch ( ClassCastException cce ) {
		throw new QueryException( "collection role is not queryable: " + role );
	}
	catch ( Exception e ) {
		throw new QueryException( "collection role not found: " + role );
	}
}
 
Example 2
Project: lams   File: AbstractEntityPersister.java   View Source Code Vote up 6 votes
@Override
public EntityPersister getSubclassEntityPersister(Object instance, SessionFactoryImplementor factory) {
	if ( !hasSubclasses() ) {
		return this;
	}
	else {
		final String concreteEntityName = getEntityTuplizer().determineConcreteSubclassEntityName(
				instance,
				factory
		);
		if ( concreteEntityName == null || getEntityName().equals( concreteEntityName ) ) {
			// the contract of EntityTuplizer.determineConcreteSubclassEntityName says that returning null
			// is an indication that the specified entity-name (this.getEntityName) should be used.
			return this;
		}
		else {
			return factory.getEntityPersister( concreteEntityName );
		}
	}
}
 
Example 3
Project: lams   File: SubqueryExpression.java   View Source Code Vote up 5 votes
@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
	final StringBuilder buf = new StringBuilder( toLeftSqlString( criteria, criteriaQuery ) );
	if ( op != null ) {
		buf.append( ' ' ).append( op ).append( ' ' );
	}
	if ( quantifier != null ) {
		buf.append( quantifier ).append( ' ' );
	}

	final SessionFactoryImplementor factory = criteriaQuery.getFactory();
	final OuterJoinLoadable persister =
			(OuterJoinLoadable) factory.getEntityPersister( criteriaImpl.getEntityOrClassName() );

	createAndSetInnerQuery( criteriaQuery, factory );
	criteriaImpl.setSession( deriveRootSession( criteria ) );

	final CriteriaJoinWalker walker = new CriteriaJoinWalker(
			persister,
			innerQuery,
			factory,
			criteriaImpl,
			criteriaImpl.getEntityOrClassName(),
			criteriaImpl.getSession().getLoadQueryInfluencers(),
			innerQuery.getRootSQLALias()
	);

	return buf.append( '(' ).append( walker.getSQLString() ).append( ')' ).toString();
}
 
Example 4
Project: lams   File: StructuredCacheEntry.java   View Source Code Vote up 5 votes
@Override
public Object destructure(Object structured, SessionFactoryImplementor factory) {
	final Map map = (Map) structured;
	final boolean lazyPropertiesUnfetched = (Boolean) map.get( "_lazyPropertiesUnfetched" );
	final String subclass = (String) map.get( "_subclass" );
	final Object version = map.get( "_version" );
	final EntityPersister subclassPersister = factory.getEntityPersister( subclass );
	final String[] names = subclassPersister.getPropertyNames();
	final Serializable[] state = new Serializable[names.length];
	for ( int i = 0; i < names.length; i++ ) {
		state[i] = (Serializable) map.get( names[i] );
	}
	return new StandardCacheEntryImpl( state, subclass, lazyPropertiesUnfetched, version );
}
 
Example 5
Project: lams   File: SessionFactoryHelper.java   View Source Code Vote up 5 votes
/**
 * Given a (potentially unqualified) class name, locate its persister.
 *
 * @param sfi The session factory implementor.
 * @param className The (potentially unqualified) class name.
 *
 * @return The defined persister for this class, or null if none found.
 */
public static Queryable findQueryableUsingImports(SessionFactoryImplementor sfi, String className) {
	final String importedClassName = sfi.getImportedClassName( className );
	if ( importedClassName == null ) {
		return null;
	}
	try {
		return (Queryable) sfi.getEntityPersister( importedClassName );
	}
	catch ( MappingException me ) {
		return null;
	}
}
 
Example 6
Project: lams   File: BulkOperationCleanupAction.java   View Source Code Vote up 5 votes
/**
 * Constructs an action to cleanup "affected cache regions" based on a
 * set of affected table spaces.  This differs from {@link #BulkOperationCleanupAction(SessionImplementor, Queryable[])}
 * in that here we have the affected <strong>table names</strong>.  From those
 * we deduce the entity persisters which are affected based on the defined
 * {@link EntityPersister#getQuerySpaces() table spaces}; and from there, we
 * determine the affected collection regions based on any collections
 * in which those entity persisters participate as elements/keys/etc.
 *
 * @param session The session to which this request is tied.
 * @param tableSpaces The table spaces.
 */
@SuppressWarnings({ "unchecked" })
public BulkOperationCleanupAction(SessionImplementor session, Set tableSpaces) {
	final LinkedHashSet<String> spacesList = new LinkedHashSet<String>();
	spacesList.addAll( tableSpaces );

	final SessionFactoryImplementor factory = session.getFactory();
	for ( String entityName : factory.getAllClassMetadata().keySet() ) {
		final EntityPersister persister = factory.getEntityPersister( entityName );
		final String[] entitySpaces = (String[]) persister.getQuerySpaces();
		if ( affectedEntity( tableSpaces, entitySpaces ) ) {
			spacesList.addAll( Arrays.asList( entitySpaces ) );

			if ( persister.hasCache() ) {
				entityCleanups.add( new EntityCleanup( persister.getCacheAccessStrategy() ) );
			}
			if ( persister.hasNaturalIdentifier() && persister.hasNaturalIdCache() ) {
				naturalIdCleanups.add( new NaturalIdCleanup( persister.getNaturalIdCacheAccessStrategy() ) );
			}

			final Set<String> roles = session.getFactory().getCollectionRolesByEntityParticipant( persister.getEntityName() );
			if ( roles != null ) {
				for ( String role : roles ) {
					final CollectionPersister collectionPersister = factory.getCollectionPersister( role );
					if ( collectionPersister.hasCache() ) {
						collectionCleanups.add(
								new CollectionCleanup( collectionPersister.getCacheAccessStrategy() )
						);
					}
				}
			}
		}
	}

	this.affectedTableSpaces = spacesList.toArray( new String[ spacesList.size() ] );
}
 
Example 7
Project: lams   File: EntityType.java   View Source Code Vote up 5 votes
protected EntityPersister getAssociatedEntityPersister(final SessionFactoryImplementor factory) {
	final EntityPersister persister = associatedEntityPersister;
	//The following branch implements a simple lazy-initialization, but rather than the canonical
	//form it returns the local variable to avoid a second volatile read: associatedEntityPersister
	//needs to be volatile as the initialization might happen by a different thread than the readers.
	if ( persister == null ) {
		associatedEntityPersister = factory.getEntityPersister( getAssociatedEntityName() );
		return associatedEntityPersister;
	}
	else {
		return persister;
	}
}
 
Example 8
Project: lams   File: EntityType.java   View Source Code Vote up 5 votes
/**
 * 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 );
}