Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

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

Prototype

public Criteria createCriteria(String associationPath) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity.

Usage

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

License:Apache License

public ProjectPE tryFindProject(final String databaseInstanceCode, final String groupCode,
        final String projectCode) {
    assert projectCode != null : "Unspecified project code.";
    assert groupCode != null : "Unspecified group code.";

    final Criteria criteria = getSession().createCriteria(ProjectPE.class);
    criteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(projectCode)));
    final Criteria groupCriteria = criteria.createCriteria("group");
    groupCriteria.add(Restrictions.eq("code", CodeConverter.tryToDatabase(groupCode)));
    if (StringUtils.isBlank(databaseInstanceCode)) {
        groupCriteria.add(Restrictions.eq("databaseInstance", getDatabaseInstance()));
    } else {//w w w .  j a  v a  2s . c om
        groupCriteria.createCriteria("databaseInstance")
                .add(Restrictions.eq("code", CodeConverter.tryToDatabase(databaseInstanceCode)));
    }
    final ProjectPE project = (ProjectPE) criteria.uniqueResult();
    return project;
}

From source file:chat.service.DoChat.java

License:LGPL

/**
 * also persist SO/*from w ww  . j a  v  a2s.  c o  m*/
 *
 * @return with {@link Chat#datime}
 */
@Service
public Chat post(Chat c, Input.Upload smiley) throws Exception {
    c.out = new User().id(sess.me);
    validate(c);

    Criteria<?> t = data.criteria(User.class).setProjection(Projections.rowCount());
    t.add(Restrictions.idEq(c.in.id));
    t.createCriteria("friends").add(Restrictions.idEq(c.out.id));
    if ((Integer) t.uniqueResult() == 0)
        throw err("You must be his/her friend");
    c.datime = new Date();
    while (smiley != null && smiley.available() > 0) {
        final Bytes b = new Bytes(smiley, false);
        final ByteArrayInputStream in = new ByteArrayInputStream(b.bytes, b.beginBi, b.byteN());
        Smiley s = new Smiley();
        s.in = c.in;
        s.image = new Blob() {
            @Override
            public long length() {
                return b.byteN();
            }

            @Override
            public void truncate(long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public byte[] getBytes(long pos, int len) {
                throw new UnsupportedOperationException();
            }

            @Override
            public int setBytes(long pos, byte[] bytes) {
                throw new UnsupportedOperationException();
            }

            @Override
            public int setBytes(long pos, byte[] bytes, int i, int j) {
                throw new UnsupportedOperationException();
            }

            @Override
            public long position(byte[] bytes, long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public InputStream getBinaryStream() {
                in.reset();
                return in;
            }

            @Override
            public OutputStream setBinaryStream(long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public long position(Blob blob, long pos) {
                throw new UnsupportedOperationException();
            }

            @Override
            public void free() {
            }

            @Override
            public InputStream getBinaryStream(long pos, long length) {
                throw new UnsupportedOperationException();
            }
        };
        s.type = smiley.type();
        if (c.smileys == null)
            c.smileys = new ArrayList<Smiley>();
        c.smileys.add(s);
        smiley.next();
    }
    data.save(c);
    return c;
}

From source file:com.abiquo.server.core.common.persistence.DefaultDAOBase.java

License:Open Source License

private Criteria createNestedCriteria(final Criteria baseCriteria, final String... propertyNames) {
    Criteria crit = baseCriteria;
    for (String property : propertyNames) {
        crit = crit.createCriteria(property);
    }//from   w w  w .  j a  v  a 2  s . c o m
    return crit;
}

From source file:com.abiquo.server.core.enterprise.UserDAO.java

License:Open Source License

private Criteria createCriteria(final Enterprise enterprise, final Role role, final String[] filters,
        final String orderBy, final boolean desc, final boolean connected) {
    Criteria criteria = createCriteria();

    if (enterprise != null) {
        criteria.add(sameEnterprise(enterprise));
    }/*  w  w w  .  jav a 2 s.  c om*/

    if (role != null) {
        criteria.add(sameRole(role));
    }
    if (!ArrayUtils.isEmpty(filters)) {
        for (String filter : filters) {
            if (!StringUtils.isEmpty(filter)) {
                criteria.add(filterBy(filter));
            }
        }
    }

    if (!StringUtils.isEmpty(orderBy)) {
        Order order = Order.asc(orderBy);
        if (desc) {
            order = Order.desc(orderBy);
        }
        criteria.addOrder(order);
    }

    if (connected) {
        criteria.createCriteria("sessions").add(Restrictions.gt("expireDate", new Date()));
        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    }
    return criteria;
}

From source file:com.algoTrader.CriteriaSearch.java

/**
 * Locates a <code>Criteria</code> for a <code>childEntityName</code>. If a
 * <code>Criteria</code> exists for the <code>childEntityName</code>, it is returned. If
 * not, one is created and referenced in the <code>childCriteriaMap</code> under the
 * <code>childEntityName</code>.
 *
 * @param childEntityName/*  w  ww.j a  v  a2  s  .c  om*/
 * @param parentCriteria
 * @return criteria The Criteria for the childEntityName.
 * @throws HibernateException
 */
private Criteria locateCriteria(String childEntityName, Criteria parentCriteria) throws HibernateException {
    if (this.childCriteriaMap.containsKey(childEntityName)) {
        return (Criteria) this.childCriteriaMap.get(childEntityName);
    }
    Criteria childCriteria = parentCriteria.createCriteria(childEntityName);
    if (this.configuration.isForceEagerLoading()) {
        parentCriteria.setFetchMode(childEntityName, FetchMode.JOIN);
    }

    // Hibernate does not support a 'unique' identifier. As a search may contain outer joins,
    // duplicates in the resultList are possible. We eliminate any duplicates here, creating a
    // distinctified resultSet (Suggestion from Hibernate itself; see www.hibernate.org's FAQ's).
    parentCriteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
    this.childCriteriaMap.put(childEntityName, childCriteria);
    return childCriteria;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@SuppressWarnings("unchecked")
@Override/*ww w  .j  av a  2s  .co  m*/
public List<Chart> getVentaVendedores(int idVendedor) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartVenta = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria facturas = getSession().createCriteria(Factura.class);
        facturas.add(Restrictions.like("estado", "CONFIRMADO"));
        facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        Criteria vendedorFactura = facturas.createCriteria("idVendedor");
        vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
        vendedorFactura.setProjection(Projections.rowCount());
        Long counter = (Long) facturas.uniqueResult();
        chart.setValue(counter.intValue());
        chart.setDate(fromDate);
        chartVenta.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartVenta;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getDineroVendedores(int idVendedor) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartVenta = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria facturas = getSession().createCriteria(Factura.class);
        facturas.add(Restrictions.like("estado", "CONFIRMADO"));
        facturas.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        Criteria vendedorFactura = facturas.createCriteria("idVendedor");
        vendedorFactura.add(Restrictions.eq("idUsuario", idVendedor));
        facturas.setProjection(Projections.sum("total"));
        BigDecimal counter = (BigDecimal) facturas.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {// ww w .j a  v a 2s .  c o  m
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartVenta.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartVenta;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getMontoMedioPago(int idMedioPago) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartMedioPago = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria metodo = getSession().createCriteria(MetodoPagoFactura.class);
        metodo.add(Restrictions.eq("estado", true));
        Criteria planPago = metodo.createCriteria("planPago");
        Criteria tarjeta = planPago.createCriteria("tarjeta");
        Criteria medioPago = tarjeta.createCriteria("medioPago");
        medioPago.add(Restrictions.eq("idMedioPago", idMedioPago));
        metodo.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        metodo.setProjection(Projections.sum("montoPago"));
        BigDecimal counter = (BigDecimal) metodo.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {//from ww w .jav a  2  s  . c o m
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartMedioPago.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartMedioPago;
}

From source file:com.ar.dev.tierra.api.dao.impl.ChartDAOImpl.java

@Override
public List<Chart> getVentaMedioPago(int idMedioPago) {
    Calendar calendarInitial = Calendar.getInstance();
    Calendar calendarClosing = Calendar.getInstance();
    calendarInitial.set(Calendar.HOUR_OF_DAY, 0);
    calendarInitial.set(Calendar.MINUTE, 0);
    calendarInitial.set(Calendar.SECOND, 0);
    calendarInitial.set(Calendar.MILLISECOND, 0);
    Date fromDate = calendarInitial.getTime();
    calendarClosing.set(Calendar.HOUR_OF_DAY, 23);
    calendarClosing.set(Calendar.MINUTE, 59);
    calendarClosing.set(Calendar.SECOND, 59);
    calendarClosing.set(Calendar.MILLISECOND, 59);
    Date toDate = calendarClosing.getTime();
    int days = 0;
    List<Chart> chartMedioPago = new ArrayList<>();
    while (days <= 6) {
        Chart chart = new Chart();
        Criteria metodo = getSession().createCriteria(MetodoPagoFactura.class);
        metodo.add(Restrictions.eq("estado", true));
        Criteria planPago = metodo.createCriteria("planPago");
        Criteria tarjeta = planPago.createCriteria("tarjeta");
        Criteria medioPago = tarjeta.createCriteria("medioPago");
        medioPago.add(Restrictions.eq("idMedioPago", idMedioPago));
        metodo.add(Restrictions.between("fechaCreacion", fromDate, toDate));
        metodo.setProjection(Projections.rowCount());
        Long counter = (Long) metodo.uniqueResult();
        if (counter != null) {
            chart.setValue(counter.intValue());
        } else {/*from ww w .  j a v  a  2s. c  om*/
            chart.setValue(0);
        }
        chart.setDate(fromDate);
        chartMedioPago.add(chart);
        calendarInitial.add(Calendar.DAY_OF_MONTH, -1);
        fromDate = calendarInitial.getTime();
        calendarClosing.add(Calendar.DAY_OF_MONTH, -1);
        toDate = calendarClosing.getTime();
        days++;
    }
    return chartMedioPago;
}

From source file:com.ar.dev.tierra.api.dao.impl.DetalleFacturaDAOImpl.java

@Override
public List<DetalleFactura> facturaDetalle(int idFactura) {
    Criteria detalleCriteria = getSession().createCriteria(DetalleFactura.class);
    Criteria facturaCriteria = detalleCriteria.createCriteria("factura");
    detalleCriteria.add(Restrictions.eq("estadoDetalle", true));
    facturaCriteria.add(Restrictions.eq("idFactura", idFactura));
    List<DetalleFactura> list = detalleCriteria.list();
    return list;/* www.ja va  2 s  .  c  o m*/
}