Example usage for org.hibernate Query setResultTransformer

List of usage examples for org.hibernate Query setResultTransformer

Introduction

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

Prototype

@Deprecated
Query<R> setResultTransformer(ResultTransformer transformer);

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private <F extends FocusType> PrismObject<F> searchShadowOwnerAttempt(String shadowOid, OperationResult result)
        throws ObjectNotFoundException {
    PrismObject<F> owner = null;//from  ww w  .j a  va  2  s.  co  m
    Session session = null;
    try {
        session = beginReadOnlyTransaction();
        Query query = session.getNamedQuery("searchShadowOwner.getShadow");
        query.setString("oid", shadowOid);
        if (query.uniqueResult() == null) {
            throw new ObjectNotFoundException("Shadow with oid '" + shadowOid + "' doesn't exist.");
        }

        LOGGER.trace("Selecting account shadow owner for account {}.", new Object[] { shadowOid });
        query = session.getNamedQuery("searchShadowOwner.getOwner");
        query.setString("oid", shadowOid);
        query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);

        List<GetObjectResult> focuses = query.list();
        LOGGER.trace("Found {} focuses, transforming data to JAXB types.",
                new Object[] { (focuses != null ? focuses.size() : 0) });

        if (focuses == null || focuses.isEmpty()) {
            // account shadow owner was not found
            return null;
        }

        if (focuses.size() > 1) {
            LOGGER.warn("Found {} owners for shadow oid {}, returning first owner.",
                    new Object[] { focuses.size(), shadowOid });
        }

        GetObjectResult focus = focuses.get(0);
        owner = updateLoadedObject(focus, (Class<F>) FocusType.class, null, session);

        session.getTransaction().commit();
    } catch (ObjectNotFoundException ex) {
        rollbackTransaction(session, ex, result, true);
        throw ex;
    } catch (SchemaException | RuntimeException ex) {
        handleGeneralException(ex, session, result);
    } finally {
        cleanupSessionAndResult(session, result);
    }

    return owner;
}

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private PrismObject<UserType> listAccountShadowOwnerAttempt(String accountOid, OperationResult result)
        throws ObjectNotFoundException {
    PrismObject<UserType> userType = null;
    Session session = null;/*w w  w .  ja v  a2 s .  co m*/
    try {
        session = beginReadOnlyTransaction();
        Query query = session.getNamedQuery("listAccountShadowOwner.getUser");
        query.setString("oid", accountOid);
        query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);

        List<GetObjectResult> users = query.list();
        LOGGER.trace("Found {} users, transforming data to JAXB types.",
                new Object[] { (users != null ? users.size() : 0) });

        if (users == null || users.isEmpty()) {
            // account shadow owner was not found
            return null;
        }

        if (users.size() > 1) {
            LOGGER.warn("Found {} users for account oid {}, returning first user. [interface change needed]",
                    new Object[] { users.size(), accountOid });
        }

        GetObjectResult user = users.get(0);
        userType = updateLoadedObject(user, UserType.class, null, session);

        session.getTransaction().commit();
    } catch (SchemaException | RuntimeException ex) {
        handleGeneralException(ex, session, result);
    } finally {
        cleanupSessionAndResult(session, result);
    }

    return userType;
}

From source file:com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl.java

License:Apache License

private <T extends ShadowType> List<PrismObject<T>> listResourceObjectShadowsAttempt(String resourceOid,
        Class<T> resourceObjectShadowType, OperationResult result)
        throws ObjectNotFoundException, SchemaException {

    List<PrismObject<T>> list = new ArrayList<>();
    Session session = null;//from   w w w  . j  a v a2 s  . co m
    try {
        session = beginReadOnlyTransaction();
        Query query = session.getNamedQuery("listResourceObjectShadows");
        query.setString("oid", resourceOid);
        query.setResultTransformer(GetObjectResult.RESULT_TRANSFORMER);

        List<GetObjectResult> shadows = query.list();
        LOGGER.debug("Query returned {} shadows, transforming to JAXB types.",
                new Object[] { (shadows != null ? shadows.size() : 0) });

        if (shadows != null) {
            for (GetObjectResult shadow : shadows) {
                PrismObject<T> prismObject = updateLoadedObject(shadow, resourceObjectShadowType, null,
                        session);
                list.add(prismObject);
            }
        }
        session.getTransaction().commit();
    } catch (SchemaException | RuntimeException ex) {
        handleGeneralException(ex, session, result);
    } finally {
        cleanupSessionAndResult(session, result);
    }

    return list;
}

From source file:com.github.dactiv.orm.core.hibernate.support.BasicHibernateDao.java

License:Apache License

/**
 * Querydistinct transformer,????distinct?
 * //w w w .j a v  a  2s  .  c  o m
 * @param queryOrNamedQuery hql HibernateNamedQuery
 * @param values 
 * 
 * @return List
 */
public <X> List<X> distinct(String queryOrNamedQuery, Object... values) {
    Query query = createQuery(queryOrNamedQuery, values);
    query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return query.list();
}

From source file:com.github.dactiv.orm.core.hibernate.support.BasicHibernateDao.java

License:Apache License

/**
 * Querydistinct transformer,????distinct?
 * //w  w  w  . j a v a2 s  . c o m
 * @param queryOrNamedQuery hql HibernateNamedQuery
 * @param values 
 * 
 * @return List
 */
public <X> List<X> distinct(String queryOrNamedQuery, Map<String, Object> values) {
    Query query = createQuery(queryOrNamedQuery, values);
    query.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

    return query.list();
}

From source file:com.griffinslogistics.bookspackage.BookspackageDAO.java

private Query createQuery(String queryString, Map<String, Object> queryParameters, Class resultClass) {
    Query query = this.sessionFactory.getCurrentSession().createQuery(queryString);

    // set query parameters
    if (queryParameters != null) {
        for (Map.Entry<String, Object> entrySet : queryParameters.entrySet()) {
            String key = entrySet.getKey();
            Object value = entrySet.getValue();
            query.setParameter(key, value);
        }//from  w  w  w.  ja v  a  2 s  .c  o  m
    }

    if (resultClass != null) {
        query.setResultTransformer(Transformers.aliasToBean(resultClass));
    }

    return query;
}

From source file:com.griffinslogistics.db.helpers.BooksHelper.java

public List<BookExtendedModel> getAllBooksByTransportation(Transportation transportation) {
    logger.log(Level.SEVERE, "{0}: getAllBooksByTransportation started", CLASS_NAME);

    this.session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = this.session.beginTransaction();

    List<BookExtendedModel> result = new ArrayList<BookExtendedModel>();

    try {/* ww w. j av  a  2 s.  c  om*/
        Query query = this.session.createQuery(QUERY_BOOK_EXTENDED_MODEL_BY_TRANSPORTATION);
        query.setParameter("transportation", transportation);
        query.setResultTransformer(Transformers.aliasToBean(BookExtendedModel.class));
        result = (List<BookExtendedModel>) query.list();

    } catch (HibernateException e) {
        transaction.rollback();
        logger.log(Level.SEVERE, e.getMessage());
    } finally {
        this.session.close();

        logger.log(Level.SEVERE, "{0}: getAllBooksByTransportation finished", CLASS_NAME);
    }

    return result;
}

From source file:com.griffinslogistics.db.helpers.BooksHelper.java

public List<BookBoxModel> getAllBookBoxModelsByPackage(Bookspackage bookspackage) {

    logger.log(Level.SEVERE, "{0}: getAllBookBoxModelsByPackage started", CLASS_NAME);

    this.session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = this.session.beginTransaction();

    List<BookBoxModel> result = new ArrayList<BookBoxModel>();

    try {/*from  ww  w  . ja  va 2s  .  c o m*/
        Query query = this.session.createQuery(QUERY_BOOK_BOX_MODELS_BY_PACKAGE);

        query.setParameter("bookspackage", bookspackage);
        query.setResultTransformer(Transformers.aliasToBean(BookBoxModel.class));
        result = (List<BookBoxModel>) query.list();

        for (BookBoxModel bookBoxModel : result) {
            bookBoxModel.setClient(bookspackage.getClient());
            bookBoxModel.setDeliveryAddress(bookspackage.getDeliveryAddress());
        }
    } catch (HibernateException e) {
        transaction.rollback();
    } finally {
        this.session.close();

        logger.log(Level.SEVERE, "{0}: getAllBookBoxModelsByPackage finished", CLASS_NAME);

    }

    return result;
}

From source file:com.griffinslogistics.db.helpers.BooksHelper.java

public Map<String, List<BookBoxModel>> getAllBookBoxModelsByTransportation(Transportation transportation) {
    logger.log(Level.SEVERE, "{0}: getAllBookBoxModelsByTransportation started", CLASS_NAME);

    Map<String, List<BookBoxModel>> result = new HashMap<String, List<BookBoxModel>>();

    this.session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = this.session.beginTransaction();

    try {/*from   ww  w .  j  a  v  a  2  s  . c o m*/
        //            transportation = (Transportation) this.session.merge(transportation);
        transportation = (Transportation) this.session.get(Transportation.class, transportation.getId());

        for (Bookspackage bookspackage : transportation.getBookspackages()) {

            Query query = this.session.createQuery(QUERY_BOOK_BOX_MODELS_BY_PACKAGE);

            query.setParameter("bookspackage", bookspackage);
            query.setResultTransformer(Transformers.aliasToBean(BookBoxModel.class));
            List<BookBoxModel> bookBoxModels = (List<BookBoxModel>) query.list();

            for (BookBoxModel bookBoxModel : bookBoxModels) {
                bookBoxModel.setClient(bookspackage.getClient());
                bookBoxModel.setDeliveryAddress(bookspackage.getDeliveryAddress());
            }

            if (bookBoxModels.size() > 0) {
                result.put(bookspackage.getPackageNumber(), bookBoxModels);
            }
        }

        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        logger.log(Level.SEVERE, e.getMessage());
    } finally {
        this.session.close();

        logger.log(Level.SEVERE, "{0}: getAllBookBoxModelsByTransportation finished", CLASS_NAME);
    }

    return result;
}

From source file:com.griffinslogistics.db.helpers.BookspackagesHelper.java

public List<BookspackageCMRModel> totalWeightForBookspackages(Transportation transportation) {
    logger.log(Level.SEVERE, "{0}: totalWeightForBookspackages started", CLASS_NAME);

    List<BookspackageCMRModel> result = new ArrayList<BookspackageCMRModel>();

    this.session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = this.session.beginTransaction();

    try {/*  www. j  av a  2 s.c  om*/
        Query query = this.session.createQuery(QUERY_TOTAL_WEIGHT_FOR_ALL_BOOKSPACKAGES);
        query.setResultTransformer(Transformers.aliasToBean(BookspackageCMRModel.class));
        query.setParameter("transportation", transportation);
        result = (List<BookspackageCMRModel>) query.list();
    } catch (HibernateException e) {
        transaction.rollback();
        BookspackagesHelper.logger.log(Level.SEVERE, e.getMessage());
    } finally {
        this.session.close();

        logger.log(Level.SEVERE, "{0}: totalWeightForBookspackages finished", CLASS_NAME);

    }

    return result;
}