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.mil.randommenu.dao.MenuItemDao.java

public List<MenuItem> getMenuItemFromMenu(Menu menu) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    criteria.add(Restrictions.eq("menu", menu));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.mil.randommenu.dao.MenuItemDao.java

public List<Vegetable> getVegetablesFromMenu(Menu menu) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    criteria.add(Restrictions.eq("menu", menu));
    criteria.setProjection(Projections.property("vegetable"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.mil.randommenu.dao.MenuItemDao.java

public List<Vegetable> getVegdetablesFromWeekMenus() {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(MenuItem.class);
    Criteria menuCriteria = criteria.createCriteria("menu");
    menuCriteria.add(Restrictions.eq("isWeekMenu", true));
    criteria.setProjection(Projections.property("vegetable"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    return criteria.list();
}

From source file:com.miranteinfo.seam.framework.service.GenericRetrieveService.java

License:Open Source License

/**
 * Retorna a lista de entidades do tipo <T> ordenada pelas propriedades informadas, nesta ordem.
 * @param <T>//from w  ww. j  ava2 s  . c o m
 * @param classe
 * @param sort
 * @param propertiesName
 * @return lista de entidades ordadenadas
 */
public <T extends BaseEntity> List<T> retrieveAll(Class<T> classe, SearchSort sort, String... propertiesName) {
    Criteria criteria = getSession().createCriteria(classe);
    if (SearchSort.ASCENDING.equals(sort)) {
        for (String property : propertiesName) {
            criteria.addOrder(Order.asc(property));
        }
    } else if (SearchSort.DESCENDING.equals(sort)) {
        for (String property : propertiesName) {
            criteria.addOrder(Order.desc(property));
        }
    }
    return criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
}

From source file:com.myapp.core.base.dao.impl.AbstractBaseDao.java

/**
 * ? ?? ?/*  w  w  w  .  j  a va2  s  .c  o m*/
 * ? ???
 */

@Deprecated
public PageModel toPageDetachedCriteria(Class claz, DetachedCriteria dca, ProjectionList pList, Integer curPage,
        Integer pageSize) throws QueryException {
    Criteria query = dca.getExecutableCriteria(getCurrentSession());
    long rowCount = ((Long) query.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    PageModel pm = new PageModel(curPage, pageSize, rowCount);
    if (pList != null) {
        query.setProjection(pList);
    } else {
        query.setProjection(null);
    }

    query.setResultTransformer(Transformers.aliasToBean(claz));
    query.setFirstResult(pm.getStartNum());
    query.setMaxResults(pageSize);
    pm.setDatas(query.list());
    return pm;
}

From source file:com.negocio.dao.ModuloDaoImpl.java

public List<Modulos> listarModulos() {
    Criteria crit = createEntityCriteria();
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Modulos> modulos = (List<Modulos>) crit.list();
    return modulos;
}

From source file:com.negocio.dao.ProductosDaoImpl.java

public List<Producto> listarProducto() {
    Criteria crit = createEntityCriteria();
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Producto> productos = (List<Producto>) crit.list();
    return productos;
}

From source file:com.negocio.dao.ProductosDaoImpl.java

public List<Producto> listarPorMarca(int idMarca) {
    Criteria crit = createEntityCriteria();
    crit.add(Restrictions.eq("prodMarcaId", idMarca));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Producto> productos = (List<Producto>) crit.list();
    return productos;
}

From source file:com.negocio.dao.UsuarioDaoImpl.java

@Override
public List<Usuarios> listarUsuarios() {
    Criteria crit = createEntityCriteria();
    crit.addOrder(Order.asc("usuApellido"));
    crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Usuarios> usuarios = (List<Usuarios>) crit.list();
    return usuarios;
}

From source file:com.painiu.core.dao.hibernate.PhotoDAOHibernate.java

License:Open Source License

static Criteria buildPhotoCriteria(final Session session, User user, String[] tags, boolean taggedAll,
        String text, Relation relation, boolean count) {
    Criteria criteria = session.createCriteria(Photo.class);

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

        if (relation != null) {
            criteria.add(/*from w  w w .  j a  va2 s  .co  m*/
                    Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", relation, UserTypes.relation()));
        }
    } else {
        criteria.add(
                Restrictions.sqlRestriction(" {alias}.privacy & ? > 0", Relation.NONE, UserTypes.relation()));

        criteria.setFetchMode("user", FetchMode.JOIN);
    }

    //if (user == null && group == null) {
    if (user == null) {
        Disjunction disjState = Restrictions.disjunction();

        disjState.add(Restrictions.eq("state", Photo.State.USER_POPULAR));
        disjState.add(Restrictions.eq("state", Photo.State.USER_COMMENDATORY));
        disjState.add(Restrictions.eq("state", Photo.State.USER_SENIOR));

        criteria.add(disjState);
    }

    //if (album != null) {
    //   criteria.createAlias("albumPhotos", "ap");
    //   criteria.add( Restrictions.eq("ap.album", album) );
    //}

    //if (group != null) {
    //   criteria.createAlias("groupPhotos", "gp");
    //   criteria.add( Restrictions.eq("gp.group", group) );
    //}

    if ((tags != null && tags.length > 0) || text != null) {
        Criteria subCriteria = criteria.createCriteria("photoTags", "tags");

        if (tags != null && tags.length > 0) {
            if (taggedAll) {
                Conjunction conj = Restrictions.conjunction();
                for (int i = 0; i < tags.length; i++) {
                    conj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(conj);
            } else {
                Disjunction disj = Restrictions.disjunction();
                for (int i = 0; i < tags.length; i++) {
                    disj.add(Restrictions.eq("tagName", tags[i]));
                }
                subCriteria.add(disj);
            }
        }

        if (text != null) {
            Disjunction disj = Restrictions.disjunction();

            disj.add(Restrictions.like("title", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.like("description", text, MatchMode.ANYWHERE));
            disj.add(Restrictions.eq("tags.tagName", text));

            criteria.add(disj);
        }
    }

    // TODO order parameters
    if (!count) {
        /*if (album != null) {
           criteria.addOrder(Order.asc("ap.position"));
        } else*/
        /*if (group != null) {
           criteria.addOrder(Order.asc("gp.position"));
        } else {*/
        criteria.addOrder(Order.desc("timestamp"));
        //}
    }
    // distinct ?
    if ((tags != null && tags.length > 1) || text != null) {
        ProjectionList proj = Projections.projectionList();

        proj.add(Projections.property("id")).add(Projections.property("title"))
                .add(Projections.property("width")).add(Projections.property("height"))
                .add(Projections.property("address.host")).add(Projections.property("address.dir"))
                .add(Projections.property("address.filename")).add(Projections.property("address.secret"))
                .add(Projections.property("address.username")).add(Projections.property("address.fileKey"));

        if (user == null) {
            criteria.createAlias("user", "user");
            proj.add(Projections.property("user.id")).add(Projections.property("user.username"))
                    .add(Projections.property("user.nickname")).add(Projections.property("user.buddyIcon.host"))
                    .add(Projections.property("user.buddyIcon.dir"))
                    .add(Projections.property("user.buddyIcon.filename"))
                    .add(Projections.property("user.buddyIcon.username"))
                    .add(Projections.property("user.buddyIcon.fileKey"));
        }

        criteria.setProjection(Projections.distinct(proj));

        criteria.setResultTransformer(new PhotoBeanResultTransformer());
    }

    return criteria;
}