Example usage for org.hibernate.criterion DetachedCriteria forClass

List of usage examples for org.hibernate.criterion DetachedCriteria forClass

Introduction

In this page you can find the example usage for org.hibernate.criterion DetachedCriteria forClass.

Prototype

public static DetachedCriteria forClass(Class clazz) 

Source Link

Document

Static builder to create a DetachedCriteria for the given entity, by its Class.

Usage

From source file:br.ufmg.hc.telessaude.diagnostico.dominio.dao.ExameDAOLocal.java

public List<Exame> consultar(Integer id, String nomePaciente, Date inicio, Date fim) throws DAOException {
    ArrayList<Criterion> restrict = new ArrayList();
    if (id != null) {
        restrict.add(Restrictions.eq("id", id));
    } else {/*from   w w w  . j  av a 2 s .  c  o m*/
        if (nomePaciente != null && !nomePaciente.isEmpty()) {
            restrict.add(Restrictions.ilike("pc.nome", nomePaciente, MatchMode.ANYWHERE));
        }
        if (inicio != null) {
            restrict.add(Restrictions.ge("datainclusao", inicio));
        }
        if (fim != null) {
            restrict.add(Restrictions.le("datainclusao", fim));
        }
    }
    try {
        final DetachedCriteria crit = DetachedCriteria.forClass(c);
        final Criteria criteria = crit.getExecutableCriteria(HibernateUtil.currentSession());

        crit.createAlias("paciente", "pc");
        crit.createAlias("status", "st");

        criteria.setProjection(Projections.projectionList().add(Projections.property("id"))
                .add(Projections.property("pc.nome")).add(Projections.property("pc.datanascimento"))
                .add(Projections.property("datainclusao")).add(Projections.property("st.nome")));

        criteria.addOrder(Order.desc("datainclusao"));

        for (final Criterion cri : restrict) {
            criteria.add(cri);
        }

        final List<Object[]> arrays = criteria.list();
        final List<Exame> exames = new ArrayList<>();

        for (Object[] array : arrays) {
            final Exame exame = new Exame(Integer.parseInt(String.valueOf(array[0])));
            exame.setPaciente(new Paciente());
            exame.getPaciente().setNome(String.valueOf(array[1]));
            exame.getPaciente().setDatanascimento((Date) array[2]);
            exame.setDatainclusao((Date) array[3]);
            exame.setStatus(new Status());
            exame.getStatus().setNome(String.valueOf(array[4]));

            exames.add(exame);
        }

        return exames;

    } catch (HibernateException ex) {
        throw new DAOException(ex.getMessage());
    }

}

From source file:br.ufmg.hc.telessaude.diagnostico.dominio.dao.LaudoDAOLocal.java

@Override
public List<Laudo> consultarPorIdExame(Integer id) throws DAOException {
    try {/* w  w w  .  j a va 2  s .co  m*/
        final DetachedCriteria crit = DetachedCriteria.forClass(c);
        final Criteria criteria = crit.getExecutableCriteria(HibernateUtil.currentSession());

        criteria.setProjection(Projections.projectionList().add(Projections.property("id"))
                .add(Projections.property("datainicio")).add(Projections.property("conteudo")));

        criteria.add(Restrictions.eq("exame.id", id));

        final List<Object[]> arrays = criteria.list();
        final List<Laudo> laudos = new ArrayList();

        for (Object[] array : arrays) {
            final Laudo laudo = new Laudo(Integer.parseInt(String.valueOf(array[0])));
            laudo.setDatainicio((Date) array[1]);
            laudo.setConteudo(array[2].toString());
            laudos.add(laudo);
        }

        return laudos;

    } catch (HibernateException ex) {
        throw new DAOException(ex.getMessage());
    }

}

From source file:ca.hec.cdm.jobs.dao.impl.CatalogDescriptionJobDaoImpl.java

License:Educational Community License

public CourseOffering getCourseOffering(String catalogNbr) {
    CourseOffering offering = null;//  w ww .j a  va 2 s .co  m

    // there should only ever be one description, but it can't hurt
    // to order by db id.
    DetachedCriteria dc = DetachedCriteria.forClass(CourseOffering.class)
            .add(Restrictions.eq("catalog_nbr", catalogNbr.toUpperCase()));

    List offList = getHibernateTemplate().findByCriteria(dc);

    if (offList != null && offList.size() != 0) {
        offering = (CourseOffering) offList.get(0);
    }

    return offering;
}

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

License:Apache License

final T tryFindTypeByCode(final String code, final boolean appendDatabaseInstance) throws DataAccessException {
    assert code != null : "Unspecified code";

    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(code)));
    if (appendDatabaseInstance) {
        criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    }//w  ww .  j a  v a  2 s.  c  om
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    final T entity = tryFindEntity(list, "type");
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s,%s): Entity type '%s' has been found.",
                MethodUtils.getCurrentMethod().getName(), code, appendDatabaseInstance, entity));
    }
    return entity;
}

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

License:Apache License

final List<T> listTypes(final boolean appendDatabaseInstance) throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    if (appendDatabaseInstance) {
        criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    }/* ww w .  j  a va  2s . c  o m*/
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        operationLog.debug(String.format("%s(%s): %d entity type(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), appendDatabaseInstance, list.size()));
    }
    return list;
}

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

License:Apache License

public List<IEntityPropertiesHolder> listEntities(final EntityTypePE entityType) throws DataAccessException {
    assert entityType != null : "Unspecified entity type.";

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityClass());
    criteria.add(Restrictions.eq(entityKind.getEntityTypeFieldName(), entityType));
    final List<IEntityPropertiesHolder> list = cast(getHibernateTemplate().findByCriteria(criteria));
    return list;//from   w  ww.  j  a v  a 2s . c  om
}

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

License:Apache License

public List<IEntityPropertiesHolder> listEntitiesWithoutPropertyValue(final EntityTypePE entityType,
        final PropertyTypePE propertyType) throws DataAccessException {
    assert entityType != null : "Unspecified entity type.";
    assert propertyType != null : "Unspecified property type.";

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityClass());
    criteria.add(Restrictions.eq(entityKind.getEntityTypeFieldName(), entityType));
    ///*from  w  w  w.j a  v a 2 s .  co m*/
    final List<IEntityPropertiesHolder> list = cast(getHibernateTemplate().findByCriteria(criteria));

    // TODO 2009-07-01, Piotr Buczek: filter results with criteria
    // final DetachedCriteria propertyTypesCriteria =
    // DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // propertyTypesCriteria.add(Restrictions.eq("entityTypePropertyType", propertyType));
    // criteria.add(Subqueries.notExists(propertyTypesCriteria.setProjection(Projections
    // .property(...))));
    final List<IEntityPropertiesHolder> result = new ArrayList<IEntityPropertiesHolder>(list.size());
    for (IEntityPropertiesHolder entity : list) {
        if (isEntityWithoutPropertyValue(entity, propertyType)) {
            result.add(entity);
        }
    }
    return result;
}

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

License:Apache License

public void fillTermUsageStatistics(List<VocabularyTermWithStats> termsWithStats, VocabularyPE vocabulary) {
    assert termsWithStats != null : "Unspecified terms.";
    assert vocabulary != null : "Unspecified vocabulary.";
    assert termsWithStats.size() == vocabulary.getTerms()
            .size() : "Sizes of terms to be filled and vocabulary terms don't match.";

    Map<Long, VocabularyTermWithStats> termsById = new HashMap<Long, VocabularyTermWithStats>(
            termsWithStats.size());/*from w  w w.  j  av a 2  s  .  c o  m*/
    for (VocabularyTermWithStats termWithStats : termsWithStats) {
        Long id = termWithStats.getTerm().getId();
        termsById.put(id, termWithStats);
    }

    final DetachedCriteria criteria = DetachedCriteria.forClass(entityKind.getEntityPropertyClass());
    // alias is the easiest way to restrict on association using criteria
    criteria.createAlias("vocabularyTerm", "term");
    criteria.add(Restrictions.eq("term.vocabularyInternal", vocabulary));
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.rowCount());
    projectionList.add(Projections.groupProperty("term.id"));
    criteria.setProjection(projectionList);

    final List<Object[]> results = cast(getHibernateTemplate().findByCriteria(criteria));

    for (Object[] result : results) {
        Integer numberOfUsages = (Integer) result[0];
        Long termId = (Long) result[1];
        termsById.get(termId).registerUsage(entityKind, numberOfUsages);
    }
}

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

License:Apache License

public final <T extends EntityTypePE> List<T> listEntityTypes() throws DataAccessException {
    final DetachedCriteria criteria = DetachedCriteria.forClass(getEntityClass());
    criteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    final String entityKindName = entityKind.name().toLowerCase();
    criteria.setFetchMode(entityKindName + "TypePropertyTypesInternal", FetchMode.JOIN);
    criteria.setResultTransformer(DetachedCriteria.DISTINCT_ROOT_ENTITY);
    final List<T> list = cast(getHibernateTemplate().findByCriteria(criteria));
    return list;//from w ww.  j  a v  a  2 s .c  o  m
}

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

License:Apache License

public List<DeletedDataSet> listDeletedDataSets(Long lastSeenDeletionEventIdOrNull) {
    final DetachedCriteria criteria = DetachedCriteria.forClass(EventPE.class);
    if (lastSeenDeletionEventIdOrNull != null) {
        criteria.add(Restrictions.gt("id", lastSeenDeletionEventIdOrNull));
    }//from  w ww  .j  av a  2  s.co  m
    criteria.add(Restrictions.eq("eventType", EventType.DELETION));
    criteria.add(Restrictions.eq("entityType", EntityType.DATASET));
    final List<EventPE> list = cast(getHibernateTemplate().findByCriteria(criteria));
    if (operationLog.isDebugEnabled()) {
        String lastDesc = lastSeenDeletionEventIdOrNull == null ? "all"
                : "id > " + lastSeenDeletionEventIdOrNull;
        operationLog.debug(String.format("%s(%d): data set deletion events(s) have been found.",
                MethodUtils.getCurrentMethod().getName(), lastDesc, list.size()));
    }
    ArrayList<DeletedDataSet> result = new ArrayList<DeletedDataSet>();
    for (EventPE event : list) {
        result.add(new DeletedDataSet(event.getIdentifier(), event.getId()));
    }
    return result;
}