org.hibernate.type.CollectionType Java Examples

The following examples show how to use org.hibernate.type.CollectionType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: cacheonix-core   Author: cacheonix   File: EvictVisitor.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
public void evictCollection(Object value, CollectionType type) {

		final Object pc;
		if ( type.hasHolder( getSession().getEntityMode() ) ) {
			pc = getSession().getPersistenceContext().removeCollectionHolder(value);
		}
		else if ( value instanceof PersistentCollection ) {
			pc = value;
		}
		else {
			return; //EARLY EXIT!
		}

		PersistentCollection collection = (PersistentCollection) pc;
		if ( collection.unsetSession( getSession() ) ) evictCollection(collection);
	}
 
Example #2
Source Project: lams   Author: lamsfoundation   File: Map.java    License: GNU General Public License v2.0 6 votes vote down vote up
public CollectionType getDefaultCollectionType() {
	if ( isSorted() ) {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.sortedMap( getRole(), getReferencedPropertyName(), getComparator() );
	}
	else if ( hasOrder() ) {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.orderedMap( getRole(), getReferencedPropertyName() );
	}
	else {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.map( getRole(), getReferencedPropertyName() );
	}
}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: Set.java    License: GNU General Public License v2.0 6 votes vote down vote up
public CollectionType getDefaultCollectionType() {
	if ( isSorted() ) {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.sortedSet( getRole(), getReferencedPropertyName(), getComparator() );
	}
	else if ( hasOrder() ) {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.orderedSet( getRole(), getReferencedPropertyName() );
	}
	else {
		return getMetadata().getTypeResolver()
				.getTypeFactory()
				.set( getRole(), getReferencedPropertyName() );
	}
}
 
Example #4
Source Project: lams   Author: lamsfoundation   File: AbstractEmptinessExpression.java    License: GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source Project: lams   Author: lamsfoundation   File: JoinHelper.java    License: GNU General Public License v2.0 6 votes vote down vote up
public JoinDefinedByMetadata createCollectionJoin(
		QuerySpace leftHandSide,
		String lhsPropertyName,
		CollectionQuerySpace rightHandSide,
		boolean rightHandSideRequired,
		CollectionType joinedPropertyType,
		SessionFactoryImplementor sessionFactory) {
	return new JoinImpl(
			leftHandSide,
			lhsPropertyName,
			rightHandSide,
			joinedPropertyType.getAssociatedJoinable( sessionFactory ).getKeyColumnNames(),
			joinedPropertyType,
			rightHandSideRequired
	);
}
 
Example #6
Source Project: lams   Author: lamsfoundation   File: AbstractEntityPersister.java    License: GNU General Public License v2.0 6 votes vote down vote up
protected Serializable getCollectionKey(
		CollectionPersister persister,
		Object owner,
		EntityEntry ownerEntry,
		SharedSessionContractImplementor session) {
	final CollectionType collectionType = persister.getCollectionType();

	if ( ownerEntry != null ) {
		// this call only works when the owner is associated with the Session, which is not always the case
		return collectionType.getKeyOfOwner( owner, session );
	}

	if ( collectionType.getLHSPropertyName() == null ) {
		// collection key is defined by the owning entity identifier
		return persister.getOwnerEntityPersister().getIdentifier( owner, session );
	}
	else {
		return (Serializable) persister.getOwnerEntityPersister().getPropertyValue( owner, collectionType.getLHSPropertyName() );
	}
}
 
Example #7
Source Project: lams   Author: lamsfoundation   File: Nullability.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * check sub elements-nullability. Returns property path that break
 * nullability or null if none
 *
 * @param propertyType type to check
 * @param value value to check
 *
 * @return property path
 * @throws HibernateException error while getting subcomponent values
 */
private String checkSubElementsNullability(Type propertyType, Object value) throws HibernateException {
	if ( propertyType.isComponentType() ) {
		return checkComponentNullability( value, (CompositeType) propertyType );
	}

	if ( propertyType.isCollectionType() ) {
		// persistent collections may have components
		final CollectionType collectionType = (CollectionType) propertyType;
		final Type collectionElementType = collectionType.getElementType( session.getFactory() );

		if ( collectionElementType.isComponentType() ) {
			// check for all components values in the collection
			final CompositeType componentType = (CompositeType) collectionElementType;
			final Iterator itr = CascadingActions.getLoadedElementsIterator( session, collectionType, value );
			while ( itr.hasNext() ) {
				final Object compositeElement = itr.next();
				if ( compositeElement != null ) {
					return checkComponentNullability( compositeElement, componentType );
				}
			}
		}
	}

	return null;
}
 
Example #8
Source Project: lams   Author: lamsfoundation   File: WrapVisitor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
Object processCollection(Object collection, CollectionType collectionType)
		throws HibernateException {

	if ( collection != null && ( collection instanceof PersistentCollection ) ) {

		final SessionImplementor session = getSession();
		PersistentCollection coll = (PersistentCollection) collection;
		if ( coll.setCurrentSession( session ) ) {
			reattachCollection( coll, collectionType );
		}
		return null;

	}
	else {
		return processArrayOrNewCollection( collection, collectionType );
	}

}
 
Example #9
Source Project: cacheonix-core   Author: cacheonix   File: PathExpressionParser.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
public void end(QueryTranslatorImpl q) throws QueryException {
	ignoreInitialJoin = false;

	Type propertyType = getPropertyType();
	if ( propertyType != null && propertyType.isCollectionType() ) {
		collectionRole = ( ( CollectionType ) propertyType ).getRole();
		collectionName = q.createNameForCollection( collectionRole );
		prepareForIndex( q );
	}
	else {
		columns = currentColumns();
		setType();
	}

	//important!!
	continuation = false;

}
 
Example #10
Source Project: lams   Author: lamsfoundation   File: DefaultRefreshEventListener.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void evictCachedCollections(Type[] types, Serializable id, EventSource source)
		throws HibernateException {
	for ( Type type : types ) {
		if ( type.isCollectionType() ) {
			CollectionPersister collectionPersister = source.getFactory().getMetamodel().collectionPersister( ( (CollectionType) type ).getRole() );
			if ( collectionPersister.hasCache() ) {
				final CollectionDataAccess cache = collectionPersister.getCacheAccessStrategy();
				final Object ck = cache.generateCacheKey(
					id,
					collectionPersister,
					source.getFactory(),
					source.getTenantIdentifier()
				);
				final SoftLock lock = cache.lockItem( source, ck, null );
				cache.remove( source, ck );
				source.getActionQueue().registerProcess( (success, session) -> cache.unlockItem( session, ck, lock ) );
			}
		}
		else if ( type.isComponentType() ) {
			CompositeType actype = (CompositeType) type;
			evictCachedCollections( actype.getSubtypes(), id, source );
		}
	}
}
 
Example #11
Source Project: lams   Author: lamsfoundation   File: PathExpressionParser.java    License: GNU General Public License v2.0 6 votes vote down vote up
public void end(QueryTranslatorImpl q) throws QueryException {
	ignoreInitialJoin = false;

	Type propertyType = getPropertyType();
	if ( propertyType != null && propertyType.isCollectionType() ) {
		collectionRole = ( ( CollectionType ) propertyType ).getRole();
		collectionName = q.createNameForCollection( collectionRole );
		prepareForIndex( q );
	}
	else {
		columns = currentColumns();
		setType();
	}

	//important!!
	continuation = false;

}
 
Example #12
Source Project: cacheonix-core   Author: cacheonix   File: AbstractEmptinessExpression.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
protected QueryableCollection getQueryableCollection(String entityName, String propertyName, SessionFactoryImplementor factory)
        throws HibernateException {
	PropertyMapping ownerMapping = ( PropertyMapping ) factory.getEntityPersister( entityName );
	Type type = ownerMapping.toType( propertyName );
	if ( !type.isCollectionType() ) {
		throw new MappingException(
		        "Property path [" + entityName + "." + propertyName + "] does not reference a collection"
		);
	}

	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 #13
Source Project: lams   Author: lamsfoundation   File: Collection.java    License: GNU General Public License v2.0 5 votes vote down vote up
public CollectionType getCollectionType() {
	if ( typeName == null ) {
		return getDefaultCollectionType();
	}
	else {
		return getMetadata().getTypeConfiguration().getTypeResolver()
				.getTypeFactory()
				.customCollection( typeName, typeParameters, role, referencedPropertyName );
	}
}
 
Example #14
Source Project: cacheonix-core   Author: cacheonix   File: IdentNode.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public void resolveIndex(AST parent) throws SemanticException {
	// An ident node can represent an index expression if the ident
	// represents a naked property ref
	//      *Note: this makes the assumption (which is currently the case
	//      in the hql-sql grammar) that the ident is first resolved
	//      itself (addrExpr -> resolve()).  The other option, if that
	//      changes, is to call resolve from here; but it is
	//      currently un-needed overhead.
	if (!(isResolved() && nakedPropertyRef)) {
		throw new UnsupportedOperationException();
	}

	String propertyName = getOriginalText();
	if (!getDataType().isCollectionType()) {
		throw new SemanticException("Collection expected; [" + propertyName + "] does not refer to a collection property");
	}

	// TODO : most of below was taken verbatim from DotNode; should either delegate this logic or super-type it
	CollectionType type = (CollectionType) getDataType();
	String role = type.getRole();
	QueryableCollection queryableCollection = getSessionFactoryHelper().requireQueryableCollection(role);

	String alias = null;  // DotNode uses null here...
	String columnTableAlias = getFromElement().getTableAlias();
	int joinType = JoinFragment.INNER_JOIN;
	boolean fetch = false;

	FromElementFactory factory = new FromElementFactory(
			getWalker().getCurrentFromClause(),
			getFromElement(),
			propertyName,
			alias,
			getFromElement().toColumns(columnTableAlias, propertyName, false),
			true
	);
	FromElement elem = factory.createCollection(queryableCollection, role, joinType, fetch, true);
	setFromElement(elem);
	getWalker().addQuerySpaces(queryableCollection.getCollectionSpaces());	// Always add the collection's query spaces.
}
 
Example #15
Source Project: cacheonix-core   Author: cacheonix   File: OnReplicateVisitor.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
Object processCollection(Object collection, CollectionType type)
		throws HibernateException {

	if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
		return null;
	}

	EventSource session = getSession();
	CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );

	if ( isUpdate ) {
		removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
	}
	if ( collection != null && ( collection instanceof PersistentCollection ) ) {
		PersistentCollection wrapper = ( PersistentCollection ) collection;
		wrapper.setCurrentSession( session );
		if ( wrapper.wasInitialized() ) {
			session.getPersistenceContext().addNewCollection( persister, wrapper );
		}
		else {
			reattachCollection( wrapper, type );
		}
	}
	else {
		// otherwise a null or brand new collection
		// this will also (inefficiently) handle arrays, which
		// have no snapshot, so we can't do any better
		//processArrayOrNewCollection(collection, type);
	}

	return null;

}
 
Example #16
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// delete does cascade to uninitialized collections
	return getAllElementsIterator( session, collectionType, collection );
}
 
Example #17
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// lock doesn't cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #18
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// refresh doesn't cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #19
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// saves / updates don't cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #20
Source Project: cacheonix-core   Author: cacheonix   File: DotNode.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public void resolveIndex(AST parent) throws SemanticException {
	if ( isResolved() ) {
		return;
	}
	Type propertyType = prepareLhs();			// Prepare the left hand side and get the data type.
	dereferenceCollection( ( CollectionType ) propertyType, true, true, null, parent );
}
 
Example #21
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// persists don't cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #22
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// persists don't cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #23
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Iterator getCascadableChildrenIterator(
		EventSource session,
		CollectionType collectionType,
		Object collection) {
	// replicate does cascade to uninitialized collections
	return getLoadedElementsIterator( session, collectionType, collection );
}
 
Example #24
Source Project: lams   Author: lamsfoundation   File: CascadingActions.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Iterate just the elements of the collection that are already there. Don't load
 * any new elements from the database.
 */
public static Iterator getLoadedElementsIterator(
		SharedSessionContractImplementor session,
		CollectionType collectionType,
		Object collection) {
	if ( collectionIsInitialized( collection ) ) {
		// handles arrays and newly instantiated collections
		return collectionType.getElementsIterator( collection, session );
	}
	else {
		// does not handle arrays (thats ok, cos they can't be lazy)
		// or newly instantiated collections, so we can do the cast
		return ((PersistentCollection) collection).queuedAdditionIterator();
	}
}
 
Example #25
Source Project: lams   Author: lamsfoundation   File: Cascade.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static void cascadeAssociation(
		final CascadingAction action,
		final CascadePoint cascadePoint,
		final EventSource eventSource,
		final int componentPathStackDepth,
		final Object parent,
		final Object child,
		final Type type,
		final CascadeStyle style,
		final Object anything,
		final boolean isCascadeDeleteEnabled) {
	if ( type.isEntityType() || type.isAnyType() ) {
		cascadeToOne( action, eventSource, parent, child, type, style, anything, isCascadeDeleteEnabled );
	}
	else if ( type.isCollectionType() ) {
		cascadeCollection(
				action,
				cascadePoint,
				eventSource,
				componentPathStackDepth,
				parent,
				child,
				style,
				anything,
				(CollectionType) type
		);
	}
}
 
Example #26
Source Project: cacheonix-core   Author: cacheonix   File: CascadingAction.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Iterate just the elements of the collection that are already there. Don't load
 * any new elements from the database.
 */
public static Iterator getLoadedElementsIterator(SessionImplementor session, CollectionType collectionType, Object collection) {
	if ( collectionIsInitialized(collection) ) {
		// handles arrays and newly instantiated collections
		return collectionType.getElementsIterator(collection, session);
	}
	else {
		// does not handle arrays (thats ok, cos they can't be lazy)
		// or newly instantiated collections, so we can do the cast
		return ( (PersistentCollection) collection ).queuedAdditionIterator();
	}
}
 
Example #27
Source Project: lams   Author: lamsfoundation   File: OnUpdateVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {

	if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
		return null;
	}

	EventSource session = getSession();
	CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );

	final Serializable collectionKey = extractCollectionKeyFromOwner( persister );
	if ( collection!=null && (collection instanceof PersistentCollection) ) {
		PersistentCollection wrapper = (PersistentCollection) collection;
		if ( wrapper.setCurrentSession(session) ) {
			//a "detached" collection!
			if ( !isOwnerUnchanged( wrapper, persister, collectionKey ) ) {
				// if the collection belonged to a different entity,
				// clean up the existing state of the collection
				removeCollection( persister, collectionKey, session );
			}
			reattachCollection(wrapper, type);
		}
		else {
			// a collection loaded in the current session
			// can not possibly be the collection belonging
			// to the entity passed to update()
			removeCollection(persister, collectionKey, session);
		}
	}
	else {
		// null or brand new collection
		// this will also (inefficiently) handle arrays, which have
		// no snapshot, so we can't do any better
		removeCollection(persister, collectionKey, session);
	}

	return null;
}
 
Example #28
Source Project: lams   Author: lamsfoundation   File: EvictVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
Object processCollection(Object collection, CollectionType type) throws HibernateException {
	if (collection != null) {
		evictCollection(collection, type);
	}

	return null;
}
 
Example #29
Source Project: lams   Author: lamsfoundation   File: OnReplicateVisitor.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object processCollection(Object collection, CollectionType type) throws HibernateException {
	if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
		return null;
	}

	final EventSource session = getSession();
	final CollectionPersister persister = session.getFactory().getMetamodel().collectionPersister( type.getRole() );

	if ( isUpdate ) {
		removeCollection( persister, extractCollectionKeyFromOwner( persister ), session );
	}
	if ( collection != null && collection instanceof PersistentCollection ) {
		final PersistentCollection wrapper = (PersistentCollection) collection;
		wrapper.setCurrentSession( (SessionImplementor) session );
		if ( wrapper.wasInitialized() ) {
			session.getPersistenceContext().addNewCollection( persister, wrapper );
		}
		else {
			reattachCollection( wrapper, type );
		}
	}
	else {
		// otherwise a null or brand new collection
		// this will also (inefficiently) handle arrays, which
		// have no snapshot, so we can't do any better
		//processArrayOrNewCollection(collection, type);
	}

	return null;

}
 
Example #30
Source Project: cacheonix-core   Author: cacheonix   File: OnUpdateVisitor.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
Object processCollection(Object collection, CollectionType type) throws HibernateException {
	
	if ( collection == CollectionType.UNFETCHED_COLLECTION ) {
		return null;
	}

	EventSource session = getSession();
	CollectionPersister persister = session.getFactory().getCollectionPersister( type.getRole() );

	final Serializable collectionKey = extractCollectionKeyFromOwner( persister );
	if ( collection!=null && (collection instanceof PersistentCollection) ) {
		PersistentCollection wrapper = (PersistentCollection) collection;
		if ( wrapper.setCurrentSession(session) ) {
			//a "detached" collection!
			if ( !isOwnerUnchanged( wrapper, persister, collectionKey ) ) {
				// if the collection belonged to a different entity,
				// clean up the existing state of the collection
				removeCollection( persister, collectionKey, session );
			}
			reattachCollection(wrapper, type);
		}
		else {
			// a collection loaded in the current session
			// can not possibly be the collection belonging
			// to the entity passed to update()
			removeCollection(persister, collectionKey, session);
		}
	}
	else {
		// null or brand new collection
		// this will also (inefficiently) handle arrays, which have
		// no snapshot, so we can't do any better
		removeCollection(persister, collectionKey, session);
	}

	return null;
}