Example usage for org.hibernate FetchMode DEFAULT

List of usage examples for org.hibernate FetchMode DEFAULT

Introduction

In this page you can find the example usage for org.hibernate FetchMode DEFAULT.

Prototype

FetchMode DEFAULT

To view the source code for org.hibernate FetchMode DEFAULT.

Click Source Link

Document

Default to the setting configured in the mapping file.

Usage

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Initializes a Criteria Query.//from   w w w  .j  a  va  2s  . c o  m
 * Mandatory Attributes:<ul>
 * <li><b>name</b>: The unqualified class name driving the criteria query.</li>
 * </ul>
 * Optional Attributes:<ul>
 * <li><b>prefix</b>: The package name of the class driving the criteria query. If null, no package is assumed.</li>
 * <li><b>maxSize</b>: The maximum number of rows to return from the database.</li>
 * <li><b>fetchSize</b>: The number of rows to fetch when rows are requested. Usually not useful for AA4H.</li>
 * <li><b>cacheEnabled</b>: Enables or disables caching for the queried objects.</li>
 * <li><b>cacheMode</b>: The cache options for the queried objects.</li>
 * <li><b>flushMode</b>: The session flush options.</li>
 * <li><b>fetchMode</b>: The collection fetch options for the query.</li>
 * <li><b>lockMode</b>: The row lock options for the queried rows.</li>
 * <li><b>timeOut</b>: The query timeout option.</li>
 * <li><b>rowCountOnly</b>: Returns a count of the query rows only.</li>
 * </ul>
 * @param attrs The attributes of the processed node.
 * @return An appended or new CriteriaSpecification
 * @throws SAXException
 */
protected CriteriaSpecification processCriteria(Attributes attrs) throws SAXException {
    if (inDetached) {
        return criteriaStack.peek();
    }
    String name = attrs.getValue("name");
    String prefix = attrs.getValue("prefix");
    if (prefix != null) {
        className = prefix + "." + name;
    } else {
        className = name;
    }
    String maxSize = attrs.getValue("maxSize");
    String fetchSize = attrs.getValue("fetchSize");
    String firstResult = attrs.getValue("firstResult");
    String cacheEnabled = attrs.getValue("cacheEnabled");
    String cacheMode = attrs.getValue("cacheMode");
    String flushMode = attrs.getValue("flushMode");
    String fetchMode = attrs.getValue("fetchMode");
    String lockMode = attrs.getValue("lockMode");
    String timeOut = attrs.getValue("timeOut");
    String rowCountOnly = attrs.getValue("rowCountOnly");
    Criteria newCriteria = null;
    try {
        if (criteriaStack.size() == 0) {
            newCriteria = session.createCriteria(className);
        } else {
            newCriteria = ((Criteria) criteriaStack.peek()).createCriteria(className);
        }
        criteriaStack.push(newCriteria);
        if ("true".equalsIgnoreCase(rowCountOnly)) {
            newCriteria.setProjection(Projections.projectionList().add(Projections.rowCount())

            );
            setRowCountOnly(true);
        }
        if (maxSize != null && isRowCountOnly() == false) {
            newCriteria.setMaxResults(Integer.parseInt(maxSize));
        }
        if (fetchSize != null && isRowCountOnly() == false) {
            newCriteria.setFetchSize(Integer.parseInt(fetchSize));
        }
        if (firstResult != null && isRowCountOnly() == false) {
            newCriteria.setFirstResult(Integer.parseInt(firstResult));
        }
        if (timeOut != null) {
            newCriteria.setTimeout(Integer.parseInt(timeOut));
        }

        if ("true".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(true);
        } else if ("false".equalsIgnoreCase(cacheEnabled)) {
            newCriteria.setCacheable(false);
        }
        if (fetchMode != null && fetchMode.length() > 0) {
            if ("JOIN".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.JOIN);
            } else if ("SELECT".equalsIgnoreCase(fetchMode)) {
                newCriteria.setFetchMode(name, FetchMode.SELECT);
            } else {
                newCriteria.setFetchMode(name, FetchMode.DEFAULT);
            }
        } else {
            newCriteria.setFetchMode(name, FetchMode.DEFAULT);
        }
        if (cacheMode != null && cacheMode.length() > 0) {
            if ("GET".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.GET);
            } else if ("IGNORE".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.IGNORE);
            } else if ("NORMAL".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            } else if ("PUT".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.PUT);
            } else if ("REFRESH".equalsIgnoreCase(cacheMode)) {
                newCriteria.setCacheMode(CacheMode.REFRESH);
            } else {
                newCriteria.setCacheMode(CacheMode.NORMAL);
            }
        }
        if (lockMode != null && lockMode.length() > 0) {
            if ("NONE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.NONE);
            } else if ("READ".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.READ);
            } else if ("UPGRADE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE);
            } else if ("UPGRADE_NOWAIT".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.UPGRADE_NOWAIT);
            } else if ("WRITE".equalsIgnoreCase(lockMode)) {
                newCriteria.setLockMode(LockMode.WRITE);
            } else {
                throw new SAXException("lockMode[" + lockMode + "] Not Recognized");
            }
        }
        if (flushMode != null && flushMode.length() > 0) {
            if ("ALWAYS".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.ALWAYS);
            } else if ("AUTO".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.AUTO);
            } else if ("COMMIT".equalsIgnoreCase(flushMode)) {
                newCriteria.setFlushMode(FlushMode.COMMIT);
            } else if ("NEVER".equalsIgnoreCase(flushMode)) {
                // NEVER is deprecated, so we won't throw an exception but we'll ignore it.
            } else {
                throw new SAXException("flushMode[" + flushMode + "] Not Recognized");
            }
        }
        return newCriteria;

    } catch (Exception e) {
        throw new SAXException("Unable to configure class " + className, e);
    }
}

From source file:com.manydesigns.portofino.persistence.hibernate.HibernateConfig.java

License:Open Source License

protected void createO2M(Configuration config, Mappings mappings, ForeignKey relationship) {

    com.manydesigns.portofino.model.database.Table manyMDTable = relationship.getFromTable();
    com.manydesigns.portofino.model.database.Table oneMDTable = relationship.getToTable();

    //Se la classe One non e' dinamica e
    // non ha la proprieta' non inserisco la relazione
    if (oneMDTable.getJavaClass() != null) {
        try {/*  ww w . j a v a2s.  co m*/
            Class oneClass = oneMDTable.getActualJavaClass();
            JavaClassAccessor accessor = JavaClassAccessor.getClassAccessor(oneClass);
            PropertyAccessor[] propertyAccessors = accessor.getProperties();
            boolean found = false;
            for (PropertyAccessor propertyAccessor : propertyAccessors) {
                if (propertyAccessor.getName().equals(relationship.getActualManyPropertyName())) {
                    found = true;
                }
            }
            if (!found) {
                logger.warn("Property '{}' not found, skipping relationship {}",
                        relationship.getActualManyPropertyName(), relationship.getQualifiedName());
                return;
            }
        } catch (Exception e) {
            //se non c'e' non inserisco la relazione
            logger.warn("Property not found, skipping relationship ", e);
            return;
        }
    }
    //relazione virtuali fra Database differenti
    if (!manyMDTable.getDatabaseName().equalsIgnoreCase(oneMDTable.getDatabaseName())) {
        logger.warn("Relationship crosses databases, skipping: {}", relationship.getQualifiedName());
        return;
    }

    String manyMDQualifiedTableName = manyMDTable.getActualEntityName();
    String oneMDQualifiedTableName = oneMDTable.getActualEntityName();

    PersistentClass clazzOne = config.getClassMapping(oneMDQualifiedTableName);
    if (clazzOne == null) {
        logger.error("Cannot find table '{}' as 'one' side of foreign key '{}'. Skipping relationship.",
                oneMDQualifiedTableName, relationship.getName());
        return;
    }

    PersistentClass clazzMany = config.getClassMapping(manyMDQualifiedTableName);
    if (clazzMany == null) {
        logger.error("Cannot find table '{}' as 'many' side of foreign key '{}'. Skipping relationship.",
                manyMDQualifiedTableName, relationship.getName());
        return;
    }

    //Uso i Bag perche' i set non funzionano con i componenti dinamici
    Bag set = new Bag(mappings, clazzOne);
    // Mettere Lazy in debug a false per ottenere subito eventuali errori
    // nelle relazioni
    set.setLazy(LAZY);

    set.setRole(
            relationship.getToTable().getActualEntityName() + "." + relationship.getActualManyPropertyName());
    //set.setNodeName(relationship.getActualManyPropertyName());
    set.setCollectionTable(clazzMany.getTable());
    OneToMany oneToMany = new OneToMany(mappings, set.getOwner());
    set.setElement(oneToMany);

    oneToMany.setReferencedEntityName(manyMDQualifiedTableName);

    oneToMany.setAssociatedClass(clazzMany);
    oneToMany.setEmbedded(true);

    set.setSorted(false);
    set.setFetchMode(FetchMode.DEFAULT);
    //Riferimenti alle colonne

    DependantValue dv;
    Table tableMany = clazzMany.getTable();
    Table tableOne = clazzOne.getTable();
    List<Column> oneColumns = new ArrayList<Column>();
    List<Column> manyColumns = new ArrayList<Column>();
    //Chiave multipla
    final List<Reference> refs = relationship.getReferences();
    if (refs.size() > 1) {
        dv = createFKComposite(mappings, relationship, manyMDTable, clazzOne, clazzMany, set, tableMany,
                tableOne, oneColumns, manyColumns);
    } else { //chiave straniera singola
        dv = createFKSingle(mappings, clazzOne, clazzMany, tableOne, oneColumns, manyColumns, refs);
    }

    tableMany.createForeignKey(relationship.getName(), manyColumns, oneMDQualifiedTableName, oneColumns);

    dv.setNullable(false);
    set.setKey(dv);
    mappings.addCollection(set);

    Property prop = new Property();
    prop.setName(relationship.getActualManyPropertyName());
    //prop.setNodeName(relationship.getActualManyPropertyName());
    prop.setValue(set);
    if (ForeignKeyConstraintType.importedKeyCascade.name().equalsIgnoreCase(relationship.getOnDelete())) {
        prop.setCascade("delete");
    } else {
        prop.setCascade("none");
    }
    clazzOne.addProperty(prop);

    //if(!StringUtils.)
}

From source file:com.smartitengineering.dao.impl.hibernate.AbstractDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
private void processNestedParameter(Criteria criteria, String element, QueryParameter parameter) {
    FetchMode mode;/*from w w w.ja  v  a  2s. c o m*/
    CompositionQueryParameter queryParameter = QueryParameterCastHelper.COMPOSITION_PARAM_FOR_NESTED_TYPE
            .cast(parameter);
    switch (queryParameter.getFetchMode()) {
    case EAGER:
        mode = FetchMode.EAGER;
        break;
    case SELECT:
        mode = FetchMode.SELECT;
        break;
    case JOIN:
        mode = FetchMode.JOIN;
        break;
    case LAZY:
        mode = FetchMode.LAZY;
        break;
    default:
    case DEFAULT:
        mode = FetchMode.DEFAULT;
        break;
    }
    criteria.setFetchMode(element, ((mode == null) ? FetchMode.JOIN : mode));
    Collection<QueryParameter> nestedParameters = queryParameter.getNestedParameters();
    if (nestedParameters == null || nestedParameters.size() <= 0) {
        return;
    }
    Criteria nestedCriteria = criteria.createCriteria(element);
    for (QueryParameter nestedQueryParameter : nestedParameters) {
        if (!nestedQueryParameter.isInitialized()) {
            continue;
        }
        processCriteria(nestedCriteria, getPropertyName(nestedQueryParameter), nestedQueryParameter);
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 * First pass to bind collection to Hibernate metamodel, sets up second pass
 *
 * @param property   The GrailsDomainClassProperty instance
 * @param collection The collection// w w  w  . j  a  v a  2  s  . c om
 * @param owner      The owning persistent class
 * @param mappings   The Hibernate mappings instance
 * @param path
 */
protected void bindCollection(GrailsDomainClassProperty property, Collection collection, PersistentClass owner,
        Mappings mappings, String path, String sessionFactoryBeanName) {

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

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

    if (pc != null && 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(mappings, collection.getOwner());
        collection.setElement(oneToMany);
        bindOneToMany(property, oneToMany, mappings);
    } else {
        bindCollectionTable(property, mappings, collection, owner.getTable(), sessionFactoryBeanName);

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

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

    // 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));
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

protected void bindOneToOne(final GrailsDomainClassProperty property, OneToOne oneToOne, String path,
        String sessionFactoryBeanName) {
    PropertyConfig config = getPropertyConfig(property);
    final GrailsDomainClassProperty otherSide = property.getOtherSide();

    oneToOne.setConstrained(otherSide.isHasOne());
    oneToOne.setForeignKeyType(oneToOne.isConstrained() ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT
            : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT);
    oneToOne.setAlternateUniqueKey(true);

    if (config != null && config.getFetch() != null) {
        oneToOne.setFetchMode(config.getFetch());
    } else {/*from w ww.java2s  . c om*/
        oneToOne.setFetchMode(FetchMode.DEFAULT);
    }

    oneToOne.setReferencedEntityName(otherSide.getDomainClass().getFullName());
    oneToOne.setPropertyName(property.getName());

    bindOneToOneInternal(property, oneToOne, path);

    if (otherSide.isHasOne()) {
        PropertyConfig pc = getPropertyConfig(property);
        bindSimpleValue(property, oneToOne, path, pc, sessionFactoryBeanName);
    } else {
        oneToOne.setReferencedPropertyName(otherSide.getName());
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.AbstractGrailsDomainBinder.java

License:Apache License

/**
 *//*  w  w w  . ja  v a2  s .  c  o  m*/
protected void bindManyToOneValues(GrailsDomainClassProperty property, ManyToOne manyToOne) {
    PropertyConfig config = getPropertyConfig(property);

    if (config != null && config.getFetch() != null) {
        manyToOne.setFetchMode(config.getFetch());
    } else {
        manyToOne.setFetchMode(FetchMode.DEFAULT);
    }

    manyToOne.setLazy(getLaziness(property));

    if (config != null) {
        manyToOne.setIgnoreNotFound(config.getIgnoreNotFound());
    }

    // set referenced entity
    manyToOne.setReferencedEntityName(property.getReferencedPropertyType().getName());
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 * First pass to bind collection to Hibernate metamodel, sets up second pass
 *
 * @param property   The GrailsDomainClassProperty instance
 * @param collection The collection/*from w ww. j a  v a 2 s.  c o m*/
 * @param owner      The owning persistent class
 * @param mappings   The Hibernate mappings instance
 * @param path
 */
private static void bindCollection(GrailsDomainClassProperty property, Collection collection,
        PersistentClass owner, Mappings mappings, String path, String sessionFactoryBeanName) {

    // set role
    String propertyName = getNameForPropertyAndPath(property, path);
    collection.setRole(StringHelper.qualify(property.getDomainClass().getFullName(), propertyName));

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

    if (pc != null && 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(mappings, collection.getOwner());
        collection.setElement(oneToMany);
        bindOneToMany(property, oneToMany, mappings);
    } else {
        bindCollectionTable(property, mappings, collection, owner.getTable(), sessionFactoryBeanName);

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

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

    // 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));
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

private static void bindOneToOne(final GrailsDomainClassProperty property, OneToOne oneToOne, String path,
        String sessionFactoryBeanName) {
    PropertyConfig config = getPropertyConfig(property);
    final GrailsDomainClassProperty otherSide = property.getOtherSide();

    oneToOne.setConstrained(otherSide.isHasOne());
    oneToOne.setForeignKeyType(oneToOne.isConstrained() ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT
            : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT);
    oneToOne.setAlternateUniqueKey(true);

    if (config != null && config.getFetch() != null) {
        oneToOne.setFetchMode(config.getFetch());
    } else {//from ww w .  ja  va  2s.  c o  m
        oneToOne.setFetchMode(FetchMode.DEFAULT);
    }

    oneToOne.setReferencedEntityName(otherSide.getDomainClass().getFullName());
    oneToOne.setPropertyName(property.getName());

    if (otherSide.isHasOne()) {
        PropertyConfig pc = getPropertyConfig(property);
        bindSimpleValue(property, oneToOne, path, pc, sessionFactoryBeanName);
    } else {
        oneToOne.setReferencedPropertyName(otherSide.getName());
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsDomainBinder.java

License:Apache License

/**
 *//* w  ww.ja  v  a2  s  . c  o  m*/
private static void bindManyToOneValues(GrailsDomainClassProperty property, ManyToOne manyToOne) {
    PropertyConfig config = getPropertyConfig(property);

    if (config != null && config.getFetch() != null) {
        manyToOne.setFetchMode(config.getFetch());
    } else {
        manyToOne.setFetchMode(FetchMode.DEFAULT);
    }

    manyToOne.setLazy(getLaziness(property));

    if (config != null) {
        manyToOne.setIgnoreNotFound(config.getIgnoreNotFound());
    }

    // set referenced entity
    manyToOne.setReferencedEntityName(property.getReferencedPropertyType().getName());
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

/**
 * Retrieves the fetch mode for the specified instance; otherwise returns the default FetchMode.
 *
 * @param object The object, converted to a string
 * @return The FetchMode//from www  .  j ava2  s . c  om
 */
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;
}