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:com.scopix.periscope.operatorqueuepriority.dao.OperatorQueuePriorityDAOImpl.java

License:Open Source License

/**
* List objects of type OperatorQueuePriority sorted by Corporate Name
* @return List<OperatorQueuePriority>
*//*from   w w w  .  jav a  2 s .  c o m*/
@Override
public List<OperatorQueuePriority> listByCorporateName() {
    log.info("executing listByCorporateName()");
    List<OperatorQueuePriority> list;
    Criteria criteria = this.getSession().createCriteria(OperatorQueuePriority.class);
    criteria.addOrder(Order.asc("corporateName"));
    criteria.addOrder(Order.asc("priority"));
    criteria.setFetchMode("userSubscriptions", FetchMode.SELECT);
    list = criteria.list();
    log.info("end)");
    return list;
}

From source file:com.scopix.periscope.operatorqueuepriority.dao.OperatorQueuePriorityDAOImpl.java

License:Open Source License

/**
 * finds a OperatorQueuePriority by corporate Id and Operator Queue Name
 * @param corporateId// ww w .j  a  v  a 2 s .  c o  m
 * @param operatorQueueName
 * @return OperatorQueuePriority
 */
@Override
public OperatorQueuePriority getByCorporateIdAndOperatorQueueName(Integer corporateId,
        String operatorQueueName) {
    Criteria criteria = this.getSession().createCriteria(OperatorQueuePriority.class);
    criteria.setMaxResults(1);
    criteria.add(Restrictions.eq("corporateId", corporateId));
    criteria.add(Restrictions.eq("operatorQueueName", operatorQueueName));
    criteria.setFetchMode("userSubscriptions", FetchMode.SELECT);
    OperatorQueuePriority operatorQueuePriority = (OperatorQueuePriority) criteria.uniqueResult();
    return operatorQueuePriority;
}

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 www.jav  a2  s  . c  om*/
    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:com.valco.dao.NotasVentaDAO.java

public List<NotasDeVenta> getNotasDeVenta() throws Exception {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*from   w  w w. j a  va  2 s.co  m*/
    List<NotasDeVenta> notas = new ArrayList<NotasDeVenta>();
    try {
        tx = session.beginTransaction();
        Criteria q = session.createCriteria(NotasDeVenta.class).setFetchMode("repartidores", FetchMode.JOIN);
        q.setFetchMode("productosInventarios", FetchMode.SELECT).setFetchMode("clientes", FetchMode.JOIN)
                .add(Restrictions.eq("estatus", "ASIGNADA"));
        notas = (List<NotasDeVenta>) q.list();
        return notas;

    } catch (HibernateException he) {
        throw new Exception("Ocurri un error al consultar los clientes.");

    } finally {
        try {
            if (session.isOpen()) {
                session.close();
            }
        } catch (HibernateException he) {
            throw new Exception("Ocurri un error al consultar los clientes.");
        }
    }
}

From source file:com.valco.dao.NotasVentaDAO.java

public List<NotasDeVenta> getAsignacionNotasDeVenta() throws Exception {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = null;/*w w  w  .j  av a  2  s . c  o  m*/
    List<NotasDeVenta> notas = new ArrayList<NotasDeVenta>();
    try {
        tx = session.beginTransaction();
        Criteria q = session.createCriteria(NotasDeVenta.class).setFetchMode("repartidores", FetchMode.JOIN);
        q.setFetchMode("productosInventarios", FetchMode.SELECT);

        notas = (List<NotasDeVenta>) q.list();
        return notas;

    } catch (HibernateException he) {
        throw new Exception("Ocurri un error al consultar los clientes.");

    } finally {
        try {
            if (session.isOpen()) {
                session.close();
            }
        } catch (HibernateException he) {
            throw new Exception("Ocurri un error al consultar los clientes.");
        }
    }
}

From source file:com.valco.dao.ProductoDAO.java

public List<Productos> getProductos() throws Exception {
    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    Transaction tx = null;//from w w w . ja  va 2s  .  c  om
    List<Productos> productos = new ArrayList<Productos>();
    try {
        tx = session.beginTransaction();
        Criteria q = session.createCriteria(Productos.class).setFetchMode("tipoProducto", FetchMode.JOIN);
        q.setFetchMode("unidadesDeMedida", FetchMode.JOIN);
        productos = (List<Productos>) q.list();
        return productos;

    } catch (HibernateException he) {
        throw new Exception("Ocurri un error al consultar los producto.");

    } finally {
        try {
            if (session.isOpen()) {
                session.close();
            }
        } catch (HibernateException he) {
            throw new Exception("Ocurri un error al consultar los producto.");
        }
    }
}

From source file:com.wavemaker.runtime.data.task.AbstractReadTask.java

License:Open Source License

protected void fetch(Criteria criteria, String propertyName) {
    criteria.setFetchMode(propertyName, FetchMode.JOIN);
}

From source file:com.yahoo.elide.datastores.hibernate3.HibernateTransaction.java

License:Apache License

private <T> void joinCriteria(Criteria criteria, final Class<T> loadClass, RequestScope scope) {
    EntityDictionary dictionary = scope.getDictionary();
    String type = dictionary.getJsonAliasFor(loadClass);
    Set<String> fields = Objects.firstNonNull(scope.getSparseFields().get(type),
            Collections.<String>emptySet());
    for (String field : fields) {
        try {/*from  ww  w.ja va  2  s. com*/
            checkFieldReadPermission(loadClass, field, scope);
            criteria.setFetchMode(field, FetchMode.JOIN);
        } catch (ForbiddenAccessException e) {
            // continue
        }
    }

    for (String include : getIncludeList(scope)) {
        criteria.setFetchMode(include, FetchMode.JOIN);
    }
}

From source file:control.dao.StaffDAO.java

public List<Object> getStaffList(String key, int type, int page) {
    int firstResult = (page - 1) * 20;
    int totalSize;
    Session session = utils.HibernateUtils.getSessionFactory().getCurrentSession();
    List<Object> resultSet = new ArrayList<>();
    boolean searchByFirstName = true;
    try {//from w  w  w  .  j av  a  2  s  . com
        session.beginTransaction();
        //            Query qr =session.createQuery("from "
        //                    +Staff.class.getName() 
        //                    + " s left join fetch s.roleCollection");
        //            session.createQuery(")
        Criteria crit = session.createCriteria(Staff.class);
        crit.setFetchMode("roleCollection", FetchMode.SELECT);
        crit.addOrder(Order.asc("id"));
        crit.setProjection(Projections.projectionList().add(Projections.property("id"))
                .add(Projections.property("userName")).add(Projections.property("firstName"))
                .add(Projections.property("lastName")).add(Projections.property("phone"))
                .add(Projections.property("email")).add(Projections.property("status")));

        switch (type) {
        case 0:
            break;

        case 1:
            crit.add(Restrictions.ilike("userName", "%" + key + "%"));
            break;

        case 2:
            while (true) {
                if (searchByFirstName) {
                    crit.add(Restrictions.ilike("firstName", "%" + key + "%"));
                    searchByFirstName = crit.list().isEmpty();
                    if (searchByFirstName) {
                        break;
                    }
                } else {
                    crit.add(Restrictions.ilike("lastName", "%" + key + "%"));
                    break;
                }
            }
            break;

        case 3:
            crit.add(Restrictions.ilike("phone", "%" + key + "%"));
            break;

        case 4:
            crit.add(Restrictions.ilike("email", "%" + key + "%"));
            break;

        case 5:
            crit.add(Restrictions.ilike("adress", "%" + key + "%"));
            break;
        }

        totalSize = crit.list().size();
        crit.setFirstResult(firstResult);
        crit.setMaxResults(20);
        resultSet = crit.list();
        resultSet.add(totalSize);
        return resultSet == null ? new ArrayList<>() : resultSet;
    } catch (Exception e) {
        e.printStackTrace();
        return new ArrayList<>();
    } finally {
        if (session.getTransaction().isActive()) {
            session.getTransaction().commit();
        }
    }
}

From source file:dao.daoVenda.java

public List<Venda> getVenda() {
    Session sessao = null;//from w  w  w  .j a v  a 2 s  .c  o  m
    String nome = "";
    try {
        sessao = dao.HibernateUtil.getSessionFactory().openSession();
        sessao.beginTransaction();

        //HQL
        //List<Cliente> res = sessao.createQuery("from Cliente cli JOIN FETCH cli.cidade ").list();
        // CRITERIA
        Criteria cons = sessao.createCriteria(Venda.class);
        cons.setFetchMode("produto", FetchMode.JOIN);
        cons.setFetchMode("vendedor", FetchMode.JOIN);

        //cons.add(Restrictions.like("idVenda", nome + "%"));

        cons.addOrder(Order.asc("idVenda"));

        List<Venda> res;
        res = cons.list();

        sessao.getTransaction().commit();
        sessao.close();
        return res;
    } catch (HibernateException he) {
        if (sessao != null) {
            sessao.getTransaction().rollback();
            sessao.close();
        }
        System.out.println("Erro ao listar as VENDAS: " + he.getMessage());
        return null;
    }
}