Java Code Examples for org.hibernate.FetchMode#JOIN

The following examples show how to use org.hibernate.FetchMode#JOIN . 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 File: JoinWalker.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Does the mapping, and Hibernate default semantics, specify that
 * this association should be fetched by outer joining
 */
protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type)
		throws MappingException {
	if ( !type.isEntityType() && !type.isCollectionType() ) {
		return false;
	}
	else {
		if ( config == FetchMode.JOIN ) {
			return true;
		}
		if ( config == FetchMode.SELECT ) {
			return false;
		}
		if ( type.isEntityType() ) {
			//TODO: look at the owning property and check that it 
			//      isn't lazy (by instrumentation)
			EntityType entityType = (EntityType) type;
			EntityPersister persister = getFactory().getEntityPersister( entityType.getAssociatedEntityName() );
			return !persister.hasProxy();
		}
		else {
			return false;
		}
	}
}
 
Example 2
Source File: JoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Does the mapping, and Hibernate default semantics, specify that
 * this association should be fetched by outer joining
 */
protected boolean isJoinedFetchEnabledInMapping(FetchMode config, AssociationType type) 
throws MappingException {
	if ( !type.isEntityType() && !type.isCollectionType() ) {
		return false;
	}
	else {
		if (config==FetchMode.JOIN) return true;
		if (config==FetchMode.SELECT) return false;
		if ( type.isEntityType() ) {
			//TODO: look at the owning property and check that it 
			//      isn't lazy (by instrumentation)
			EntityType entityType =(EntityType) type;
			EntityPersister persister = getFactory().getEntityPersister( entityType.getAssociatedEntityName() );
			return !persister.hasProxy();
		}
		else {
			return false;
		}
	}
}
 
Example 3
Source File: AnnotationBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static FetchMode getFetchMode(FetchType fetch) {
	if ( fetch == FetchType.EAGER ) {
		return FetchMode.JOIN;
	}
	else {
		return FetchMode.SELECT;
	}
}
 
Example 4
Source File: CollectionBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static void checkFilterConditions(Collection collValue) {
	//for now it can't happen, but sometime soon...
	if ( ( collValue.getFilters().size() != 0 || StringHelper.isNotEmpty( collValue.getWhere() ) ) &&
			collValue.getFetchMode() == FetchMode.JOIN &&
			!( collValue.getElement() instanceof SimpleValue ) && //SimpleValue (CollectionOfElements) are always SELECT but it does not matter
			collValue.getElement().getFetchMode() != FetchMode.JOIN ) {
		throw new MappingException(
				"@ManyToMany or @CollectionOfElements defining filter or where without join fetching "
						+ "not valid within collection using join fetching[" + collValue.getRole() + "]"
		);
	}
}
 
Example 5
Source File: GrailsHibernateQueryUtils.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieves the fetch mode for the specified instance; otherwise returns the default FetchMode.
 *
 * @param object The object, converted to a string
 * @return The FetchMode
 */
public static FetchMode getFetchMode(Object object) {
    String name = object != null ? object.toString() : "default";
    if (name.equalsIgnoreCase(FetchMode.JOIN.toString()) || name.equalsIgnoreCase("eager")) {
        return FetchMode.JOIN;
    }
    if (name.equalsIgnoreCase(FetchMode.SELECT.toString()) || name.equalsIgnoreCase("lazy")) {
        return FetchMode.SELECT;
    }
    return FetchMode.DEFAULT;
}
 
Example 6
Source File: OneToMany.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public FetchMode getFetchMode() {
	return FetchMode.JOIN;
}
 
Example 7
Source File: CriteriaJoinWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected JoinType getJoinType(
		OuterJoinLoadable persister,
		final PropertyPath path,
		int propertyNumber,
		AssociationType associationType,
		FetchMode metadataFetchMode,
		CascadeStyle metadataCascadeStyle,
		String lhsTable,
		String[] lhsColumns,
		final boolean nullable,
		final int currentDepth) throws MappingException {
	final JoinType resolvedJoinType;
	if ( translator.isJoin( path.getFullPath() ) ) {
		resolvedJoinType = translator.getJoinType( path.getFullPath() );
	}
	else {
		if ( translator.hasProjection() ) {
			resolvedJoinType = JoinType.NONE;
		}
		else {
			String fullPathWithAlias = path.getFullPath();
			String rootAlias = translator.getRootCriteria().getAlias();
			String rootAliasPathPrefix = rootAlias + ".";
			if (rootAlias != null && !fullPathWithAlias.startsWith(rootAliasPathPrefix)) {
				fullPathWithAlias = rootAliasPathPrefix + fullPathWithAlias;
			}

			FetchMode fetchMode = translator.getRootCriteria().getFetchMode( fullPathWithAlias );
			if ( isDefaultFetchMode( fetchMode ) ) {
				if ( persister != null ) {
					if ( isJoinFetchEnabledByProfile( persister, path, propertyNumber ) ) {
						if ( isDuplicateAssociation( lhsTable, lhsColumns, associationType ) ) {
							resolvedJoinType = JoinType.NONE;
						}
						else if ( isTooDeep( currentDepth ) || ( associationType.isCollectionType() && isTooManyCollections() ) ) {
							resolvedJoinType = JoinType.NONE;
						}
						else {
							resolvedJoinType = getJoinType( nullable, currentDepth );
						}
					}
					else {
						resolvedJoinType = super.getJoinType(
								persister,
								path,
								propertyNumber,
								associationType,
								metadataFetchMode,
								metadataCascadeStyle,
								lhsTable,
								lhsColumns,
								nullable,
								currentDepth
						);
					}
				}
				else {
					resolvedJoinType = super.getJoinType(
							associationType,
							metadataFetchMode,
							path,
							lhsTable,
							lhsColumns,
							nullable,
							currentDepth,
							metadataCascadeStyle
					);

				}
			}
			else {
				if ( fetchMode == FetchMode.JOIN ) {
					isDuplicateAssociation(
							lhsTable,
							lhsColumns,
							associationType
					); //deliberately ignore return value!
					resolvedJoinType = getJoinType( nullable, currentDepth );
				}
				else {
					resolvedJoinType = JoinType.NONE;
				}
			}
		}
	}
	return resolvedJoinType;
}
 
Example 8
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
/**
 * First pass to bind collection to Hibernate metamodel, sets up second pass
 *
 * @param property   The GrailsDomainClassProperty instance
 * @param collection The collection
 * @param owner      The owning persistent class
 * @param mappings   The Hibernate mappings instance
 * @param path
 */
protected void bindCollection(ToMany property, Collection collection,
                              PersistentClass owner, InFlightMetadataCollector mappings, String path, String sessionFactoryBeanName) {

    // set role
    String propertyName = getNameForPropertyAndPath(property, path);
    collection.setRole(qualify(property.getOwner().getName(), propertyName));

    PropertyConfig pc = getPropertyConfig(property);
    // configure eager fetching
    final FetchMode fetchMode = pc.getFetchMode();
    if (fetchMode == FetchMode.JOIN) {
        collection.setFetchMode(FetchMode.JOIN);
    }
    else if (pc.getFetchMode() != null) {
        collection.setFetchMode(pc.getFetchMode());
    }
    else {
        collection.setFetchMode(FetchMode.DEFAULT);
    }

    if (pc.getCascade() != null) {
        collection.setOrphanDelete(pc.getCascade().equals(CASCADE_ALL_DELETE_ORPHAN));
    }
    // if it's a one-to-many mapping
    if (shouldBindCollectionWithForeignKey(property)) {
        OneToMany oneToMany = new OneToMany(metadataBuildingContext, collection.getOwner());
        collection.setElement(oneToMany);
        bindOneToMany((org.grails.datastore.mapping.model.types.OneToMany) property, oneToMany, mappings);
    } else {
        bindCollectionTable(property, mappings, collection, owner.getTable(), sessionFactoryBeanName);

        if (!property.isOwningSide()) {
            collection.setInverse(true);
        }
    }

    if (pc.getBatchSize() != null) {
        collection.setBatchSize(pc.getBatchSize());
    }

    // set up second pass
    if (collection instanceof org.hibernate.mapping.Set) {
        mappings.addSecondPass(new GrailsCollectionSecondPass(property, mappings, collection, sessionFactoryBeanName));
    }
    else if (collection instanceof org.hibernate.mapping.List) {
        mappings.addSecondPass(new ListSecondPass(property, mappings, collection, sessionFactoryBeanName));
    }
    else if (collection instanceof org.hibernate.mapping.Map) {
        mappings.addSecondPass(new MapSecondPass(property, mappings, collection, sessionFactoryBeanName));
    }
    else { // Collection -> Bag
        mappings.addSecondPass(new GrailsCollectionSecondPass(property, mappings, collection, sessionFactoryBeanName));
    }
}
 
Example 9
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static void bindManyToManySubelements(
        Collection collection,
        Element manyToManyNode,
        Mappings model) throws MappingException {
	// Bind the where
	Attribute where = manyToManyNode.attribute( "where" );
	String whereCondition = where == null ? null : where.getValue();
	collection.setManyToManyWhere( whereCondition );

	// Bind the order-by
	Attribute order = manyToManyNode.attribute( "order-by" );
	String orderFragment = order == null ? null : order.getValue();
	collection.setManyToManyOrdering( orderFragment );

	// Bind the filters
	Iterator filters = manyToManyNode.elementIterator( "filter" );
	if ( ( filters.hasNext() || whereCondition != null ) &&
	        collection.getFetchMode() == FetchMode.JOIN &&
	        collection.getElement().getFetchMode() != FetchMode.JOIN ) {
		throw new MappingException(
		        "many-to-many defining filter or where without join fetching " +
		        "not valid within collection using join fetching [" + collection.getRole() + "]"
			);
	}
	while ( filters.hasNext() ) {
		final Element filterElement = ( Element ) filters.next();
		final String name = filterElement.attributeValue( "name" );
		String condition = filterElement.getTextTrim();
		if ( StringHelper.isEmpty(condition) ) condition = filterElement.attributeValue( "condition" );
		if ( StringHelper.isEmpty(condition) ) {
			condition = model.getFilterDefinition(name).getDefaultFilterCondition();
		}
		if ( condition==null) {
			throw new MappingException("no filter condition found for filter: " + name);
		}
		log.debug(
				"Applying many-to-many filter [" + name +
				"] as [" + condition +
				"] to role [" + collection.getRole() + "]"
			);
		collection.addManyToManyFilter( name, condition );
	}
}
 
Example 10
Source File: OneToMany.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public FetchMode getFetchMode() {
	return FetchMode.JOIN;
}
 
Example 11
Source File: CriteriaJoinWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected int getJoinType(
		AssociationType type, 
		FetchMode config, 
		String path,
		String lhsTable,
		String[] lhsColumns,
		boolean nullable,
		int currentDepth, CascadeStyle cascadeStyle)
throws MappingException {

	if ( translator.isJoin(path) ) {
		return translator.getJoinType( path );
	}
	else {
		if ( translator.hasProjection() ) {
			return -1;
		}
		else {
			FetchMode fetchMode = translator.getRootCriteria()
				.getFetchMode(path);
			if ( isDefaultFetchMode(fetchMode) ) {
				return super.getJoinType(
						type, 
						config, 
						path, 
						lhsTable, 
						lhsColumns, 
						nullable,
						currentDepth, cascadeStyle
					);
			}
			else {
				if ( fetchMode==FetchMode.JOIN ) {
					isDuplicateAssociation(lhsTable, lhsColumns, type); //deliberately ignore return value!
					return getJoinType(nullable, currentDepth);
				}
				else {
					return -1;
				}
			}
		}
	}
}