Example usage for org.hibernate Criteria setFetchMode

List of usage examples for org.hibernate Criteria setFetchMode

Introduction

In this page you can find the example usage for org.hibernate Criteria setFetchMode.

Prototype

public Criteria setFetchMode(String associationPath, FetchMode mode) throws HibernateException;

Source Link

Document

Specify an association fetching strategy for an association or a collection of values.

Usage

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

public List<StudyCalendar> searchStudyCalenderList(StudyCalendar studyCalendar) {
    List<StudyCalendar> list = new ArrayList<StudyCalendar>();
    Criteria criteria = getSession().createCriteria(StudyCalendar.class);

    criteria.add(Restrictions.eq("study", studyCalendar.getStudy()));

    if (studyCalendar.getName() != null) {
        criteria.add(Restrictions.ilike("name", studyCalendar.getName(), MatchMode.ANYWHERE));
    }// w  w w  .j a v  a2 s  .  co m

    if (studyCalendar.getStartDate() != null) {
        criteria.add(Restrictions.ge("startDate", studyCalendar.getStartDate()));
    }

    if (studyCalendar.getEndDate() != null) {
        criteria.add(Restrictions.le("endDate", studyCalendar.getEndDate()));
    }

    criteria.setFetchMode("studyComp", FetchMode.JOIN);

    list = criteria.list();

    return list;
}

From source file:br.com.rhmanager.daoImpl.FuncionarioDAOImpl.java

@Override
public List<Funcionario> getFuncionariosBusca(String nome, String cpf, Cargo cargo) {
    Session session = null;//  w  w w  .j ava2 s  .  co  m
    try {
        session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(Funcionario.class);

        if (nome.length() > 0) {
            criteria.add(Restrictions.ilike("nome", "%" + nome + "%"));
        }

        if (cpf.length() > 0) {
            criteria.add(Restrictions.ilike("cpf", cpf + "%"));
        }

        criteria.setFetchMode("cargos", FetchMode.JOIN);

        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (!session.isConnected()) {
            session.close();
        }
    }
}

From source file:ch.astina.hesperid.dao.hibernate.FilterGridDataSource.java

License:Apache License

@Override
protected void applyAdditionalConstraints(Criteria criteria) {
    for (String join : joins) {
        criteria.setFetchMode(join, FetchMode.JOIN);
    }/*from ww w .  j  av a 2  s. c  om*/

    for (Entry<String, String> alias : aliases.entrySet()) {
        criteria.createAlias(alias.getKey(), alias.getValue());
    }

    for (Criterion filter : filters) {
        criteria.add(filter);
    }

    if (order != null) {
        criteria.addOrder(order);
    }
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.AbstractGenericEntityDAO.java

License:Apache License

public final T tryGetByTechId(final TechId techId, String... connections) throws DataAccessException {
    assert techId != null : "Technical identifier unspecified.";
    final Criteria criteria = getSession().createCriteria(getEntityClass());
    criteria.add(Restrictions.eq("id", techId.getId()));
    for (String connection : connections) {
        criteria.setFetchMode(connection, FetchMode.JOIN);
    }//from   ww w.  ja  va  2  s. c om
    final T result = tryGetEntity(criteria.uniqueResult());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(
                String.format("%s(%s): '%s'.", MethodUtils.getCurrentMethod().getName(), techId, result));
    }
    return result;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DataStoreDAO.java

License:Apache License

public List<DataStorePE> listDataStores() {
    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    criteria.setFetchMode("servicesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    final List<DataStorePE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%d data stores have been found.", list.size()));
    }//  w  w  w.  j  a  v  a 2  s  . c o m
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.ExperimentDAO.java

License:Apache License

public ExperimentPE tryFindByCodeAndProject(final ProjectPE project, final String experimentCode) {
    assert experimentCode != null : "Unspecified experiment code.";
    assert project != null : "Unspecified project.";

    final Criteria criteria = getSession().createCriteria(getEntityClass());
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(experimentCode)));
    criteria.add(Restrictions.eq("projectInternal", project));
    criteria.setFetchMode("experimentType.experimentTypePropertyTypesInternal", FetchMode.JOIN);
    final ExperimentPE experiment = (ExperimentPE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog/*from  ww w.  j a  va2s  .  com*/
                .debug(String.format("Following experiment '%s' has been found for code '%s' and project '%s'.",
                        experiment, experimentCode, project));
    }
    return experiment;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.MaterialDAO.java

License:Apache License

public List<MaterialPE> listMaterialsWithPropertiesAndInhibitor(final MaterialTypePE materialType)
        throws DataAccessException {
    assert materialType != null : "Unspecified material type.";

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("materialType", materialType));
    final int count = DAOUtils.getCount(criteria);
    if (count <= DAOUtils.MAX_COUNT_FOR_PROPERTIES) {
        criteria.setFetchMode("materialProperties", FetchMode.JOIN);
    } else {// w w w . java2  s.  c o  m
        operationLog.info(String.format("Found %d materials, disable properties loading.", count));
    }
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<MaterialPE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%d materials have been found for material type '%s'.", list.size(),
                materialType));
    }
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.MaterialDAO.java

License:Apache License

public MaterialPE tryFindMaterial(MaterialIdentifier identifier) {
    assert identifier != null : "identifier not given";

    String code = CodeConverter.tryToDatabase(identifier.getCode());
    String typeCode = CodeConverter.tryToDatabase(identifier.getTypeCode());

    final Criteria criteria = getSession().createCriteria(ENTITY_CLASS);
    criteria.add(Restrictions.eq("code", code));
    criteria.createCriteria("materialType").add(Restrictions.eq("code", typeCode));
    criteria.setFetchMode("materialType.materialTypePropertyTypesInternal", FetchMode.JOIN);
    final MaterialPE material = (MaterialPE) criteria.uniqueResult();
    if (operationLog.isDebugEnabled()) {
        operationLog//from  ww  w .  j  a v  a  2 s  .c  o m
                .debug(String.format("Following material '%s' has been found for " + "code '%s' and type '%s'.",
                        material, code, typeCode));
    }
    return material;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.PropertyTypeDAO.java

License:Apache License

public List<PropertyTypePE> listAllPropertyTypesWithRelations() {
    final Criteria criteria = getSession().createCriteria(PropertyTypePE.class);
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    criteria.setFetchMode("materialTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("sampleTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("experimentTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setFetchMode("dataSetTypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    final List<PropertyTypePE> list = cast(criteria.list());
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(): %d property types(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), list.size()));
    }//w  w w  .  j  a  v a2s  .  co  m
    return list;
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.SampleDAO.java

License:Apache License

private final Criteria createListSampleForTypeCriteria(final SampleTypePE sampleType) {
    final Criteria criteria = createListAllSamplesCriteria();
    criteria.add(Restrictions.eq("sampleType", sampleType));
    fetchRelations(criteria, "container", sampleType.getContainerHierarchyDepth());
    fetchRelations(criteria, "generatedFrom", sampleType.getGeneratedFromHierarchyDepth());

    criteria.setFetchMode("experimentInternal", FetchMode.JOIN);

    return criteria;
}