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:info.jtrac.hibernate.HibernateJtracDao.java

License:Apache License

public List<Item> findAllItems(final int firstResult, final int batchSize) {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
        public Object doInHibernate(Session session) {
            session.clear();//  w w  w . j av a2s  . c  om
            Criteria criteria = session.createCriteria(Item.class);
            criteria.setCacheMode(CacheMode.IGNORE);
            criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            criteria.setFetchMode("history", FetchMode.JOIN);
            criteria.add(Restrictions.ge("id", (long) firstResult));
            criteria.add(Restrictions.lt("id", (long) firstResult + batchSize));
            return criteria.list();
        }
    });
}

From source file:info.jtrac.repository.HibernateJtracDao.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/*from ww  w.j a  va 2 s.c om*/
@Transactional(propagation = Propagation.SUPPORTS)
public List<Item> findAllItems(final int firstResult, final int batchSize) {
    entityManager.clear();
    Session session = getSession();
    Criteria criteria = session.createCriteria(Item.class);
    criteria.setCacheMode(CacheMode.IGNORE);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.setFetchMode("history", FetchMode.JOIN);
    criteria.add(Restrictions.ge("id", (long) firstResult));
    criteria.add(Restrictions.lt("id", (long) firstResult + batchSize));
    return criteria.list();
}

From source file:io.github.jonestimd.finance.dao.hibernate.HibernateDao.java

License:Open Source License

protected <T extends ENTITY> Criteria buildGetAllCriteria(Class<T> criteriaClass, String... associations) {
    Criteria criteria = getSession().createCriteria(criteriaClass);
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    criteria.setCacheable(true);/*w  w w.  j av a 2s. c  om*/
    for (String path : associations) {
        criteria.setFetchMode(path, FetchMode.JOIN);
    }
    return criteria;
}

From source file:ispyb.server.common.daos.shipping.Dewar3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*from ww  w  . ja  va  2 s .  c  om*/
public List<Dewar3VO> findFiltered(Integer proposalId, Integer shippingId, String type, String code,
        String barCode, String comments, Date date1, Date date2, String dewarStatus, String storageLocation,
        Integer dewarId, Integer experimentId, boolean fetchSession, boolean fetchDewarHistory,
        boolean fetchContainer) {
    try {
        Session session = (Session) this.entityManager.getDelegate();
        Criteria criteria = session.createCriteria(Dewar3VO.class);

        if (dewarId != null) {
            criteria.add(Restrictions.eq("dewarId", dewarId));
        }

        if (experimentId != null) {
            Criteria sessionCriteria = criteria.createCriteria("sessionVO");
            sessionCriteria.add(Restrictions.eq("sessionId", experimentId));
        }
        if (proposalId != null || shippingId != null || (date1 != null) || (date2 != null)) {

            Criteria shippingCriteria = criteria.createCriteria("shippingVO");
            if (proposalId != null) {

                Criteria proposalCriteria = shippingCriteria.createCriteria("proposalVO");
                proposalCriteria.add(Restrictions.eq("proposalId", proposalId));
            }

            if (shippingId != null) {
                shippingCriteria.add(Restrictions.eq("shippingId", shippingId));
            }

            if ((date1 != null) || (date2 != null)) {
                if (date1 != null)
                    shippingCriteria.add(Restrictions.ge("creationDate", date1));
                if (date2 != null)
                    shippingCriteria.add(Restrictions.le("creationDate", date2));
            }
        }

        if (type != null && !type.isEmpty()) {
            criteria.add(Restrictions.like("type", type));
        }

        if (code != null && !code.isEmpty()) {
            criteria.add(Restrictions.like("code", code));
        }

        if (barCode != null && !barCode.isEmpty()) {
            criteria.add(Restrictions.like("barCode", barCode));
        }

        if (comments != null && !comments.isEmpty()) {
            criteria.add(Restrictions.like("comments", comments));
        }

        if (dewarStatus != null && !dewarStatus.isEmpty()) {
            criteria.add(Restrictions.like("dewarStatus", dewarStatus));
        }

        if (storageLocation != null && !storageLocation.isEmpty()) {
            criteria.add(Restrictions.like("storageLocation", storageLocation));
        }

        if (fetchSession) {
            criteria.setFetchMode("sessionVO", FetchMode.JOIN);
            criteria.createCriteria("sessionVO");
        }

        if (fetchDewarHistory) {
            criteria.setFetchMode("dewarTransportHistoryVOs", FetchMode.JOIN);
        }

        if (fetchContainer) {
            criteria.setFetchMode("containerVOs", FetchMode.JOIN);
        }

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

        return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    } catch (Exception exp) {
        exp.printStackTrace();
        return null;
    }

}

From source file:ispyb.server.common.daos.shipping.Dewar3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Dewar3VO> findByDateWithHistory(java.sql.Date firstDate) {

    Session session = (Session) this.entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Dewar3VO.class);
    Criteria shippingCriteria = criteria.createCriteria("shippingVO");

    if (firstDate != null)
        shippingCriteria.add(Restrictions.ge("creationDate", firstDate));

    criteria.addOrder(Order.desc("dewarId"));
    criteria.setFetchMode("dewarTransportHistoryVOs", FetchMode.JOIN);

    List<Dewar3VO> dewars = criteria.list();
    // dewars = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

    return dewars;
}

From source file:ispyb.server.common.daos.shipping.Shipping3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/*  ww  w.  ja  v a2  s.  c  o  m*/
public List<Shipping3VO> findByStatus(String status, java.util.Date dateStart, boolean fetchDewars) {
    Session session = (Session) this.entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Shipping3VO.class);
    if (status != null && !status.isEmpty()) {
        criteria.add(Restrictions.like("shippingStatus", status));
    }

    if (dateStart != null) {
        criteria.add(Restrictions.ge("creationDate", dateStart));
    }

    if (fetchDewars) {
        criteria.setFetchMode("dewarVOs", FetchMode.JOIN);
    }
    criteria.addOrder(Order.desc("shippingId")).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); // DISTINCT
    // RESULTS !

    return criteria.list();
}

From source file:ispyb.server.common.daos.shipping.Shipping3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w w  w . j  a v a  2 s .  co m*/
public List<Shipping3VO> findByBeamLineOperator(String operatorSiteNumber, boolean fetchDewars) {
    Session session = (Session) this.entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Shipping3VO.class);

    if (operatorSiteNumber != null) {
        Criteria sessionCriteria = criteria.createCriteria("sessions")
                .add(Restrictions.eq("operatorSiteNumber", operatorSiteNumber));
        // .addOrder(Order.desc(propertyName))
        if (fetchDewars) {
            criteria.setFetchMode("dewarVOs", FetchMode.JOIN);
        }
        criteria.addOrder(Order.desc("creationDate"));
        criteria.addOrder(Order.desc("shippingId"));
        criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return criteria.list();
    } else
        return null;
}

From source file:ispyb.server.common.daos.shipping.Shipping3DAOBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w w  w. ja  v  a 2s.  c  om
public List<Shipping3VO> findFiltered(Integer proposalId, String shippingName, String proposalCode,
        Integer proposalNumber, String mainProposer, Date dateStart, Date dataEnd, String operatorSiteNumber,
        String type, boolean withDewars) {

    Session session = (Session) this.entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Shipping3VO.class);

    if ((shippingName != null) && (!shippingName.isEmpty())) {
        criteria.add(Restrictions.like("shippingName", shippingName));
    }

    if (dateStart != null) {
        criteria.add(Restrictions.ge("creationDate", dateStart));
    }

    if (dataEnd != null) {
        criteria.add(Restrictions.le("creationDate", dataEnd));
    }

    if (type != null) {
        criteria.add(Restrictions.like("shippingType", type));
    }

    if (proposalId != null || proposalCode != null || mainProposer != null) {
        Criteria subCritProposal = criteria.createCriteria("proposalVO");
        if (proposalId != null) {
            subCritProposal.add(Restrictions.eq("proposalId", proposalId));
        }
        if (proposalId == null && proposalCode != null && !proposalCode.isEmpty()) {
            subCritProposal.add(Restrictions.like("code", proposalCode).ignoreCase());
        }
        if (proposalId == null && proposalNumber != null)
            subCritProposal.add(Restrictions.like("number", proposalNumber).ignoreCase());
        if ((mainProposer != null) && (!mainProposer.isEmpty())) {
            Criteria subCritPerson = subCritProposal.createCriteria("personVO");
            subCritPerson.add(Restrictions.like("familyName", mainProposer).ignoreCase());
        }

    }

    if (operatorSiteNumber != null) {
        criteria.createAlias("sessions", "se");
        criteria.add(Restrictions.eq("se.operatorSiteNumber", operatorSiteNumber));
    }

    if (withDewars) {
        criteria.setFetchMode("dewarVOs", FetchMode.JOIN);
    }
    criteria.addOrder(Order.desc("creationDate"));
    criteria.addOrder(Order.desc("shippingId"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:ispyb.server.common.services.shipping.Dewar3ServiceBean.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override/* w  w  w  .ja v a  2s .c o  m*/
public List<Dewar3VO> findFiltered(final Integer proposalId, final Integer shippingId, final String type,
        final String code, final String barCode, final String comments, final Date date1, final Date date2,
        final String dewarStatus, final String storageLocation, final Integer dewarId,
        final Integer firstExperimentId, final boolean fetchSession, final boolean withDewarHistory,
        final boolean withContainer) throws Exception {

    try {
        Session session = (Session) this.entityManager.getDelegate();
        Criteria criteria = session.createCriteria(Dewar3VO.class);

        if (dewarId != null) {
            criteria.add(Restrictions.eq("dewarId", dewarId));
        }

        if (firstExperimentId != null) {
            Criteria sessionCriteria = criteria.createCriteria("sessionVO");
            sessionCriteria.add(Restrictions.eq("sessionId", firstExperimentId));
        }
        if (proposalId != null || shippingId != null || (date1 != null) || (date2 != null)) {

            Criteria shippingCriteria = criteria.createCriteria("shippingVO");
            if (proposalId != null) {

                Criteria proposalCriteria = shippingCriteria.createCriteria("proposalVO");
                proposalCriteria.add(Restrictions.eq("proposalId", proposalId));
            }

            if (shippingId != null) {
                shippingCriteria.add(Restrictions.eq("shippingId", shippingId));
            }

            if ((date1 != null) || (date2 != null)) {
                if (date1 != null)
                    shippingCriteria.add(Restrictions.ge("creationDate", date1));
                if (date2 != null)
                    shippingCriteria.add(Restrictions.le("creationDate", date2));
            }
        }

        if (type != null && !type.isEmpty()) {
            criteria.add(Restrictions.like("type", type));
        }

        if (code != null && !code.isEmpty()) {
            criteria.add(Restrictions.like("code", code));
        }

        if (barCode != null && !barCode.isEmpty()) {
            criteria.add(Restrictions.like("barCode", barCode));
        }

        if (comments != null && !comments.isEmpty()) {
            criteria.add(Restrictions.like("comments", comments));
        }

        if (dewarStatus != null && !dewarStatus.isEmpty()) {
            criteria.add(Restrictions.like("dewarStatus", dewarStatus));
        }

        if (storageLocation != null && !storageLocation.isEmpty()) {
            criteria.add(Restrictions.like("storageLocation", storageLocation));
        }

        if (fetchSession) {
            criteria.setFetchMode("sessionVO", FetchMode.JOIN);
            criteria.createCriteria("sessionVO");
        }

        if (withDewarHistory) {
            criteria.setFetchMode("dewarTransportHistoryVOs", FetchMode.JOIN);
        }

        if (withContainer) {
            criteria.setFetchMode("containerVOs", FetchMode.JOIN);
        }

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

        return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
    } catch (Exception exp) {
        exp.printStackTrace();
        return null;
    }

}

From source file:ispyb.server.common.services.shipping.Dewar3ServiceBean.java

License:Open Source License

public List<Dewar3VO> findByDateWithHistory(final java.sql.Date firstDate) throws Exception {

    Session session = (Session) this.entityManager.getDelegate();
    Criteria criteria = session.createCriteria(Dewar3VO.class);
    Criteria shippingCriteria = criteria.createCriteria("shippingVO");

    if (firstDate != null)
        shippingCriteria.add(Restrictions.ge("creationDate", firstDate));

    criteria.addOrder(Order.desc("dewarId"));
    criteria.setFetchMode("dewarTransportHistoryVOs", FetchMode.JOIN);

    List<Dewar3VO> dewars = criteria.list();
    // dewars = criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

    return dewars;
}