Example usage for org.springframework.dao.support DataAccessUtils uniqueResult

List of usage examples for org.springframework.dao.support DataAccessUtils uniqueResult

Introduction

In this page you can find the example usage for org.springframework.dao.support DataAccessUtils uniqueResult.

Prototype

@Nullable
public static <T> T uniqueResult(@Nullable Collection<T> results)
        throws IncorrectResultSizeDataAccessException 

Source Link

Document

Return a unique result object from the given Collection.

Usage

From source file:pe.gob.mef.gescon.hibernate.impl.UserDaoImpl.java

@Override
public TuserPerfil getPerfilByUser(BigDecimal idusuario) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(TuserPerfil.class);
    criteria.add(Restrictions.eq("nusuarioid", idusuario));
    return (TuserPerfil) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
}

From source file:de.forsthaus.backend.dao.impl.CustomerDAOImpl.java

@SuppressWarnings("unchecked")
@Override//from  ww w  . j  a v a  2  s  .  co m
public Customer getCustomerByOrder(de.forsthaus.backend.model.Order order) {
    DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);

    criteria.createAlias("orders", "au");
    criteria.add(Restrictions.eq("au.id", Long.valueOf(order.getId())));

    return (Customer) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
}

From source file:com.apress.progwt.server.dao.hibernate.UserDAOHibernateImpl.java

/**
 * add all fetch mode concerns to the critera. without
 * DISTINCT_ROOT_ENTITY these fetches will create multiple rows
 * /*from  w  w  w  . j ava  2 s .  c o  m*/
 * If we join both schoolRankings & processTypes, we'll get duplicates
 * again. Fetch one of the collections with a SELECT and initialize
 * instead. Test in UserServiceImpl.testFetch()
 * 
 * Same with the process of each schoolRanking. Note, this is N+1
 * selects.
 * 
 * @param crit
 * @return
 */
private User fetchAllUser(DetachedCriteria crit) {
    crit.setFetchMode("schoolRankings", FetchMode.JOIN).setFetchMode("schoolRankings.school", FetchMode.JOIN)
            .setFetchMode("schoolRankings.process", FetchMode.SELECT)
            .setFetchMode("processTypes", FetchMode.SELECT)
            .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    User rtn = (User) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(crit));
    Hibernate.initialize(rtn.getProcessTypes());
    Hibernate.initialize(rtn.getRatingTypes());
    for (Application application : rtn.getSchoolRankings()) {
        Hibernate.initialize(application.getProcess());
        Hibernate.initialize(application.getRatings());
    }

    log.debug("fetched user: " + rtn.getNickname() + " ratings " + rtn.getRatingTypes().size());

    // Hibernate.initialize(rtn.getSchoolRankings());
    return rtn;

}

From source file:br.msf.commons.persistence.springframework.validation.AnnotatedEntityValidator.java

private void validateUnique(final String pathPrefix, final Field field, final T target, final Errors errors)
        throws Exception {
    field.setAccessible(true);//from   w  w w.  ja v  a 2  s . c  o m
    final Object fieldValue = field.get(target);
    final String fieldPath = getFullPath(pathPrefix, field.getName());
    if (!errors.hasFieldErrors(fieldPath)) {
        final Unique unique = field.getAnnotation(Unique.class);
        final Column column = field.getAnnotation(Column.class);
        final JoinColumn joinColumn = field.getAnnotation(JoinColumn.class);
        final String errorCode = unique != null ? unique.errorCode() : Validator.ERROR_EXISTS;
        final boolean isUnique = (unique != null && unique.value()) || (column != null && column.unique())
                || (joinColumn != null && joinColumn.unique());
        if (isUnique && getCommandService() != null) {
            Collection<T> found = getCommandService().findByProperty(field.getName(), fieldValue);
            if (!CollectionUtils.isEmptyOrSingleton(found)) {
                throw new IllegalStateException(
                        "Inconsistent database: expecting unique result. Returned mutiple.");
            }
            final T entity = DataAccessUtils.uniqueResult(found);

            if (entity != null && (target.getId() == null || !entity.getId().equals(target.getId()))) {
                errors.rejectValue(fieldPath, errorCode, new Object[] { field.getName(), fieldValue },
                        errorCode);
            }
        }
    }
}

From source file:fr.gael.dhus.database.dao.ProductDao.java

@SuppressWarnings("unchecked")
public Product getProductByOrigin(final String origin) {
    List<Product> products = find(
            "from Product where origin='" + DaoUtils.secureString(origin) + "' AND processed = true");
    try {//from   ww w.ja  v  a  2 s  . com
        return DataAccessUtils.uniqueResult(products);
    } catch (IncorrectResultSizeDataAccessException e) {
        // Case of more than one product found.
        logger.warn("More than one entry of product origin found in database: " + origin);
        return products.get(0);
    }
}

From source file:fr.gael.dhus.database.dao.ProductDao.java

public Product getProductByUuid(String uuid, User user) {
    @SuppressWarnings("unchecked")
    Product product = (Product) DataAccessUtils
            .uniqueResult(find("from Product p where p.uuid='" + uuid + "' AND p.processed=true"));
    return product;
}

From source file:pe.gob.mef.gescon.hibernate.impl.WikiDaoImpl.java

@Override
public Tconocimiento getWikiById(BigDecimal idtipo, BigDecimal id) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(Tconocimiento.class);
    criteria.add(Restrictions.eq("ntipoconocimientoid", idtipo));
    criteria.add(Restrictions.eq("nconocimientoid", id));
    return (Tconocimiento) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
}

From source file:fr.gael.dhus.database.dao.ProductDao.java

public User getOwnerOfProduct(final Product product) {
    return (User) DataAccessUtils
            .uniqueResult(getHibernateTemplate().find("select p.owner from Product p where p=?", product));
}

From source file:pe.gob.mef.gescon.hibernate.impl.ConocimientoDaoImpl.java

@Override
public Tconocimiento getBpracticaById(BigDecimal idtipo, BigDecimal id) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(Tconocimiento.class);
    criteria.add(Restrictions.eq("ntipoconocimientoid", idtipo));
    criteria.add(Restrictions.eq("nconocimientoid", id));
    return (Tconocimiento) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
}

From source file:pe.gob.mef.gescon.hibernate.impl.ConocimientoDaoImpl.java

@Override
public Tconocimiento getOmejoraById(BigDecimal idtipo, BigDecimal id) throws Exception {
    DetachedCriteria criteria = DetachedCriteria.forClass(Tconocimiento.class);
    criteria.add(Restrictions.eq("ntipoconocimientoid", idtipo));
    criteria.add(Restrictions.eq("nconocimientoid", id));
    return (Tconocimiento) DataAccessUtils.uniqueResult(getHibernateTemplate().findByCriteria(criteria));
}