Example usage for org.hibernate Criteria setResultTransformer

List of usage examples for org.hibernate Criteria setResultTransformer

Introduction

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

Prototype

public Criteria setResultTransformer(ResultTransformer resultTransformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.arg.arsoft.siantluis.repository.imp.PaymentRepository.java

@Override
public Map findByQuery(PaymentQuery query) {
    int pageSize = Configs.PAGE_SIZE;
    Criteria criteria = factory.getCurrentSession().createCriteria(Payment.class);
    if (query.getCode() != null && !query.getCode().equals("")) {
        if (query.getCode().contains("*") || query.getCode().contains("?")) {
            criteria.add(Restrictions.like("code", query.getCode().replace("*", "%").replace("?", "_")));
        } else {//from  w w w. ja v  a  2s.c om
            criteria.add(Restrictions.eq("code", query.getCode()));
        }
    }
    if (query.getName() != null && !query.getName().equals("")) {
        if (query.getName().contains("*") || query.getName().contains("?")) {
            criteria.add(Restrictions.like("name", query.getName().replace("*", "%").replace("?", "_")));
        } else {
            criteria.add(Restrictions.eq("name", query.getName()));
        }
    }
    if (query.getPaymentBy() != null && !query.getPaymentBy().equals("")) {
        criteria.createAlias("paymentBy", "p", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("p.code", query.getCode()));
    }
    if (query.getPaymentDateFrom() != null) {
        criteria.add(Restrictions.ge("paymentDate", query.getPaymentDateFrom()));
    }
    if (query.getPaymentDateTo() != null) {
        criteria.add(Restrictions.le("paymentDate", query.getPaymentDateTo()));
    }
    if (query.getDepartment() != null && query.getDepartment() != 0) {
        criteria.createAlias("department", "d", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("d.id", query.getDepartment()));
    }
    if (query.getBranch() != null && query.getBranch() != 0) {
        criteria.createAlias("branch", "b", JoinType.LEFT_OUTER_JOIN);
        criteria.add(Restrictions.eq("b.id", query.getBranch()));
    }
    long totalRecord = (long) criteria.setProjection(Projections.count(Projections.id().toString()))
            .uniqueResult();

    criteria.setProjection(null);

    int start = ((query.getPage() - 1) * pageSize);

    // fetch assco
    criteria.setFetchMode("paymentBy", FetchMode.JOIN);
    criteria.setFetchMode("department", FetchMode.JOIN);
    criteria.setFetchMode("branch", FetchMode.JOIN);

    List<Payment> result = criteria.setResultTransformer(Criteria.ROOT_ENTITY).setFirstResult(start)
            .setMaxResults(pageSize).list();

    long totalPage = totalRecord / pageSize;
    if ((totalRecord % pageSize) > 0) {
        totalPage++;
    }
    List<Integer> pages = new ArrayList<Integer>();
    for (int index = 1; index <= totalPage; index++) {
        pages.add(index);
    }

    Map data = new HashMap();
    data.put("list", result);
    data.put("totalPage", totalPage);
    data.put("totalRecord", totalRecord);
    data.put("pages", pages);

    System.out.println(pages.size());
    return data;
}

From source file:com.arg.arsoft.siantluis.repository.imp.ProductRepository.java

@Override
public Product findByKey(Integer key) {
    Criteria criteria = factory.getCurrentSession().createCriteria(Product.class);
    criteria.add(Restrictions.eq("id", key));
    criteria.setFetchMode("supplier", FetchMode.JOIN);
    List<Product> result = criteria.setResultTransformer(Criteria.ROOT_ENTITY).list();
    if (result != null && result.size() > 0) {
        return result.get(0);
    } else {//w  w w.j a  v  a2  s.com
        return null;
    }
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> getConfirmedPost(int first, int items) {
    SQLQuery sqlQuery = getSession().createSQLQuery("");

    Criteria criteria = getSession().createCriteria(SellingPost.class)
            .add(Restrictions.eq("status", SellingPostStatus.CONFIRM));

    //Ly s dng//from  w w  w.  j  a  va2 s.  c om
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).addOrder(Order.desc("id"))
            .setFirstResult(first).setMaxResults(items);

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))).addOrder(Order.desc("id"));

    Set<SellingPost> posts = new HashSet(subCriteria.list());
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(rowCount, items, first, posts);

    return paginationData;
}

From source file:com.bookselling.dao.SellingPostDaoImpl.java

@Override
public PaginationData<SellingPost> getBySubject(int first, int items, Subject subject) {
    Integer subjectId = subject.getId();
    Criteria criteria = getSession().createCriteria(SellingPost.class);
    criteria.createAlias("purchasingSellingEntity", "slen").createAlias("slen.subjects", "sbj")
            .add(Restrictions.eq("status", SellingPostStatus.CONFIRM))
            .add(Restrictions.eq("sbj.id", subjectId));

    //Ly s dng/*from  ww  w. j  a  v a 2  s  .c om*/
    long rowCount = (long) criteria.setProjection(Projections.countDistinct("id")).uniqueResult();

    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setProjection(Projections.distinct(Projections.id())).addOrder(Order.desc("id"))
            .setFirstResult(first).setMaxResults(items);

    List<Integer> ids = new ArrayList<>();
    for (Iterator<Integer> temp = criteria.list().iterator(); temp.hasNext();)
        ids.add(temp.next());

    //Criteria ph
    Criteria subCriteria = getSession().createCriteria(SellingPost.class);
    subCriteria.createAlias("purchasingSellingEntity", "bk").createAlias("bk.subjects", "sbj")
            .createAlias("bk.publisher", "pub").createAlias("seller", "sl").createAlias("sl.account", "acc")
            .add(Restrictions.in("id", ids.size() > 0 ? ids : Arrays.asList(-1))).addOrder(Order.desc("id"));

    Set<SellingPost> posts = new HashSet<>(subCriteria.list());
    HibernateInitSupport.setCls(SellingPost.class);
    for (SellingPost post : posts)
        HibernateInitSupport.initDomain(post);

    PaginationData paginationData = new PaginationData(rowCount, items, first, posts);

    return paginationData;
}

From source file:com.bryan.crud.dao.impl.AmbitoDAOImpl.java

@Override
public List<Ambito> findAllAmbitos() {
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return (List<Ambito>) criteria.list();
}

From source file:com.bryan.crud.dao.impl.UserDaoImpl.java

@Override
public List<User> findAllUsers() {
    Criteria criteria = createEntityCriteria();
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<User> users = (List<User>) criteria.list();

    return users;
}

From source file:com.cfs.controller.AuthoritiesDAO.java

public List<Authorities> listaAuthorities() throws Exception {
    List<Authorities> lista = null;
    try {//  w  w w. j  a  v  a  2s.c o m
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Authorities.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        lista = criteria.list();
        //System.out.println(Utilities.getInstance().getLineNumber() + "RESULTADO DO PRIMEIRO - "+lista.get(1).getAuthority());
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return lista;
}

From source file:com.cfs.controller.AuthoritiesDAO.java

public Authorities buscaAuthorities(String authority) throws Exception {
    Authorities retorno = null;/* w w w .ja va  2s  .  c o  m*/
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Authorities.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.like("authority", authority));
        if (criteria.list().size() > 1) {
            System.out.println(Utilities.getInstance().getLineNumber() + "[buscaAuthorities (" + authority
                    + ")][ERRO] criteria.list().size() = " + criteria.list().size());
        }
        retorno = (Authorities) criteria.uniqueResult();
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return retorno;
}

From source file:com.cfs.controller.UsersDAO.java

public List<Users> listaUsers() throws Exception {
    List<Users> lista = null;
    try {//from   w w w  .  j a va  2s .c  o  m
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Users.class);
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        lista = criteria.list();
        //System.out.println(Utilities.getInstance().getLineNumber()+"RESULTADO DO PRIMEIRO"+lista.get(0).getDescricao());
    } catch (Exception e) {
        throw new Exception(e.getMessage());
    } finally {
        session.close();
    }
    return lista;
}

From source file:com.cfs.controller.UsersDAO.java

public Users buscaUsuarioPorUSERNAME(String username) {
    usuario = null;// w  ww . j  a  v a 2s.  c  om
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(Users.class);
        criteria.add(Restrictions.eq("username", username));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        usuario = (Users) criteria.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        session.close();
    }
    return usuario;
}