Example usage for org.hibernate SQLQuery setResultTransformer

List of usage examples for org.hibernate SQLQuery setResultTransformer

Introduction

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

Prototype

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

Source Link

Document

Set a strategy for handling the query results.

Usage

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public PageFinder<ApiKey> queryApiKey(ApiKeyMetadata apiKeyMetadata, Query query) throws Exception {
    StringBuilder sqlPrefixBuilder = new StringBuilder();
    sqlPrefixBuilder.append(" select ");
    sqlPrefixBuilder.append(/*  w ww  .j  ava 2 s.  c o m*/
            " t1.id, t1.app_key as appKey, t1.app_secret as appSecret, t1.status, t1.update_user as updateUser, t1.update_time as updateTime ");
    StringBuilder sqlSuffixBuilder = new StringBuilder();
    sqlSuffixBuilder.append(" from ");
    sqlSuffixBuilder.append(" tbl_merchant_api_key t1 ");
    sqlSuffixBuilder.append(" left join ");
    sqlSuffixBuilder.append(" tbl_merchant_api_key_metadata t2 ");
    sqlSuffixBuilder.append(" on(t1.id = t2.key_id) ");
    sqlSuffixBuilder.append(" left join ");
    sqlSuffixBuilder.append(" ( ");
    sqlSuffixBuilder.append(apiKeyDao.getApiKeyPotentialCustomersSqlStatement());
    sqlSuffixBuilder.append(" ) t3 ");
    sqlSuffixBuilder.append(" on (t2.metadata_val = t3.metadata_val) ");
    sqlSuffixBuilder.append(" where 1 = 1 ");

    // ?
    Map<String, Object> properties = new HashMap<String, Object>();
    if (apiKeyMetadata != null) {
        if (StringUtils.isNotBlank(apiKeyMetadata.getMetadataTag())) {
            sqlSuffixBuilder.append(" and t3.metadata_tag like :metadata_tag");
            properties.put("metadata_tag", '%' + apiKeyMetadata.getMetadataTag() + '%');
        }
        if (apiKeyMetadata.getMetadataKey() != null) {
            sqlSuffixBuilder.append(" and t2.metadata_key = :metadata_key ");
            properties.put("metadata_key", apiKeyMetadata.getMetadataKey().name());
        }
        if (StringUtils.isNotBlank(apiKeyMetadata.getMetadataVal())) {
            sqlSuffixBuilder.append(" and t2.metadata_val = :metadata_val ");
            properties.put("metadata_val", apiKeyMetadata.getMetadataVal());
        }
        if (apiKeyMetadata.getApiKey() != null) {
            if (apiKeyMetadata.getApiKey().getStatus() != null) {
                sqlSuffixBuilder.append(" and t1.status = :status ");
                properties.put("status", apiKeyMetadata.getApiKey().getStatus().ordinal());
            }
        }
    }

    Session session = null;
    PageFinder<ApiKey> pageFinder = null;
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery("select count(1) " + sqlSuffixBuilder.toString());
        sqlQuery.setProperties(properties);
        int rowCount = ((Number) sqlQuery.uniqueResult()).intValue();
        pageFinder = new PageFinder<ApiKey>(query.getPage(), query.getPageSize(), rowCount);
        if (rowCount > 0) {
            // ?
            sqlSuffixBuilder.append(" group by ");
            sqlSuffixBuilder
                    .append(" t1.id, t1.app_key, t1.app_secret, t1.status, t1.update_user, t1.update_time ");
            sqlSuffixBuilder.append(" order by ");
            sqlSuffixBuilder.append(" t1.update_time desc ");
            // ?APP
            sqlQuery = session.createSQLQuery(sqlPrefixBuilder.append(sqlSuffixBuilder).toString());
            sqlQuery.setFirstResult(pageFinder.getStartOfPage());
            sqlQuery.setMaxResults(pageFinder.getPageSize());
            sqlQuery.setProperties(properties);
            sqlQuery.addScalar("id", Hibernate.STRING);
            sqlQuery.addScalar("appKey", Hibernate.STRING);
            sqlQuery.addScalar("appSecret", Hibernate.STRING);
            sqlQuery.addScalar("status", customApiKeyStatus);
            sqlQuery.addScalar("updateTime", Hibernate.STRING);
            sqlQuery.addScalar("updateUser", Hibernate.STRING);
            sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKey.class));
            List<ApiKey> merchantApiKeys = sqlQuery.list();
            // ?APP?
            sqlPrefixBuilder.setLength(0);
            sqlPrefixBuilder.append(" select ");
            sqlPrefixBuilder.append(
                    " t1.metadata_key as metadataKey, t1.metadata_val as metadataVal, t2.metadata_tag as metadataTag ");
            sqlPrefixBuilder.append(" from ");
            sqlPrefixBuilder.append(" tbl_merchant_api_key_metadata t1 ");
            sqlPrefixBuilder.append(" left join ");
            sqlPrefixBuilder.append(" ( ");
            sqlPrefixBuilder.append(apiKeyDao.getApiKeyPotentialCustomersSqlStatement());
            sqlPrefixBuilder.append(" ) t2 ");
            sqlPrefixBuilder.append(" on(t1.metadata_val = t2.metadata_val) ");
            sqlPrefixBuilder.append(" where ");
            sqlPrefixBuilder.append(" t1.key_id = ? ");
            sqlQuery = session.createSQLQuery(sqlPrefixBuilder.toString());
            sqlQuery.addScalar("metadataKey", customAppType);
            sqlQuery.addScalar("metadataVal", Hibernate.STRING);
            sqlQuery.addScalar("metadataTag", Hibernate.STRING);
            sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
            for (ApiKey merchantApiKey : merchantApiKeys) {
                sqlQuery.setParameter(0, merchantApiKey.getId());
                merchantApiKey.setApiKeyMetadatas(new HashSet<ApiKeyMetadata>(sqlQuery.list()));
            }
            pageFinder.setData(merchantApiKeys);
        }
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
    return pageFinder;
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public List<ApiKeyMetadata> queryApiKeyCustomers(String apiKeyId) throws Exception {
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append(" select ");
    sqlBuilder.append(//  ww w.  j av  a 2s .  c  o  m
            " t1.metadata_key as metadataKey, t1.metadata_val as metadataVal, t1.metadata_tag as metadataTag ");
    sqlBuilder.append(" from ");
    sqlBuilder.append(" ( ");
    sqlBuilder.append(apiKeyDao.getApiKeyPotentialCustomersSqlStatement());
    sqlBuilder.append(" ) t1 ");
    sqlBuilder.append(" inner join ");
    sqlBuilder.append(" tbl_merchant_api_key_metadata t2 ");
    sqlBuilder.append(" on(t1.metadata_val = t2.metadata_val) ");
    sqlBuilder.append(" where ");
    sqlBuilder.append(" t2.key_id = ? ");

    Session session = null;
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.setParameter(0, apiKeyId);
        sqlQuery.addScalar("metadataKey", customAppType);
        sqlQuery.addScalar("metadataVal", Hibernate.STRING);
        sqlQuery.addScalar("metadataTag", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
        return sqlQuery.list();
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public List<ApiKeyMetadata> queryApiKeyPotentialCustomers() throws Exception {
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append(" select ");
    sqlBuilder.append(/*from  w ww .ja v a2s .c o m*/
            " t1.metadata_key as metadataKey, t1.metadata_val as metadataVal, t1.metadata_tag as metadataTag ");
    sqlBuilder.append(" from ");
    sqlBuilder.append(" ( ");
    sqlBuilder.append(apiKeyDao.getApiKeyPotentialCustomersSqlStatement());
    sqlBuilder.append(" ) t1 ");
    sqlBuilder.append(" left join ");
    sqlBuilder.append(" tbl_merchant_api_key_metadata t2 ");
    sqlBuilder.append(" on(t1.metadata_val = t2.metadata_val) ");
    sqlBuilder.append(" where ");
    sqlBuilder.append(" t2.id is null ");

    Session session = null;
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.addScalar("metadataKey", customAppType);
        sqlQuery.addScalar("metadataVal", Hibernate.STRING);
        sqlQuery.addScalar("metadataTag", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
        return sqlQuery.list();
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public List<ApiKeyMetadata> queryApiKeyByType(String type) throws Exception {
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append(" SELECT km.metadata_key as metadataKey,km.metadata_val as metadataVal ");
    sqlBuilder.append(" FROM tbl_merchant_api_key_metadata km ");
    sqlBuilder.append(" WHERE km.metadata_key = ? ");

    Session session = null;//from ww w  . jav  a2s.c o m
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.setParameter(0, type);
        sqlQuery.addScalar("metadataKey", customAppType);
        sqlQuery.addScalar("metadataVal", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
        return sqlQuery.list();
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public List<ApiKeyMetadata> queryApiKeyByapiKeyIdAndType(String apiKeyId, String type) throws Exception {
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append(" SELECT km.metadata_key as metadataKey,km.metadata_val as metadataVal ");
    sqlBuilder.append(" FROM tbl_merchant_api_key_metadata km ");
    sqlBuilder.append(" WHERE km.key_id = ? ");
    sqlBuilder.append(" AND km.metadata_key = ? ");

    Session session = null;/*from  ww w  .  j av a 2 s  .  co m*/
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.setParameter(0, apiKeyId);
        sqlQuery.setParameter(1, type);
        sqlQuery.addScalar("metadataKey", customAppType);
        sqlQuery.addScalar("metadataVal", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
        return sqlQuery.list();
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

@Override
public ApiKey randomSeekTestApiKey() throws Exception {
    Session session = null;//  w  w  w  .ja v  a 2  s .co m
    try {
        StringBuilder sqlBuilder = new StringBuilder();
        sqlBuilder.append(" select ");
        sqlBuilder.append(" t1.app_key as appKey, t1.app_secret as appSecret ");
        sqlBuilder.append(" from ");
        sqlBuilder.append(" tbl_merchant_api_key t1 ");
        sqlBuilder.append(" inner join ");
        sqlBuilder.append(" tbl_merchant_api_key_metadata t2 ");
        sqlBuilder.append(" on(t1.id = t2.key_id) ");
        sqlBuilder.append(" left join ");
        sqlBuilder.append(" ( ");
        sqlBuilder.append(apiKeyDao.getApiKeyPotentialCustomersSqlStatement());
        sqlBuilder.append(" ) t3 ");
        sqlBuilder.append(" on(t2.metadata_val = t3.metadata_val) ");
        sqlBuilder.append(" where ");
        sqlBuilder.append(" t3.metadata_tag like '%%' ");
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.addScalar("appKey", Hibernate.STRING);
        sqlQuery.addScalar("appSecret", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKey.class));
        List<ApiKey> list = sqlQuery.list();
        return list.size() == 0 ? null : list.get(new Random().nextInt(list.size()));
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.yougou.api.service.impl.ApiKeyServiceImpl.java

/**
 * ?metadataValApiKeyMetadata?/* w  w w . ja  v a2s.  co  m*/
 * @param metadataVal
 * @return
 * @throws Exception
 */
@Override
public ApiKeyMetadata queryApiKeyByMetadataVal(String metadataVal) throws Exception {
    StringBuilder sqlBuilder = new StringBuilder();
    sqlBuilder.append(" SELECT km.metadata_key as metadataKey,km.metadata_val as metadataVal,km.key_id as id ");
    sqlBuilder.append(" FROM tbl_merchant_api_key_metadata km ");
    sqlBuilder.append(" WHERE km.metadata_key = 'MERCHANTS' ");
    sqlBuilder.append(" AND km.metadata_val = ? ");

    Session session = null;
    try {
        session = apiKeyDao.getHibernateSession();
        SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
        sqlQuery.setParameter(0, metadataVal);
        sqlQuery.addScalar("metadataKey", customAppType);
        sqlQuery.addScalar("metadataVal", Hibernate.STRING);
        sqlQuery.addScalar("id", Hibernate.STRING);
        sqlQuery.setResultTransformer(Transformers.aliasToBean(ApiKeyMetadata.class));
        List<ApiKeyMetadata> list = sqlQuery.list();
        return list.size() == 0 ? null : list.get(0);
    } finally {
        apiKeyDao.releaseHibernateSession(session);
    }
}

From source file:com.zdtx.ifms.specific.service.monitor.IpCamManager.java

/**
 * Get IP Camera's users/*from   w w  w  . j a  v a  2 s .  c om*/
 * @return   CamUserVO
 */
@SuppressWarnings("unchecked")
public CamUserVO getCamUsers() {
    String sql = "SELECT A.USERNAME AS ADMINNAME, A.USERPASS AS ADMINPASS, "
            + "B.USERNAME AS OPERATORNAME, B.USERPASS AS OPERATORPASS, "
            + "C.USERNAME AS VIEWERNAME, C.USERPASS AS VIEWERPASS "
            + "FROM T_CORE_CAM_USER A, T_CORE_CAM_USER B, T_CORE_CAM_USER C "
            + "WHERE A.AUTHLEVEL = 0 AND B.AUTHLEVEL = 1 AND C.AUTHLEVEL = 2";
    SQLQuery query = dao.getSession().createSQLQuery(sql);
    query.addScalar("adminName", StringType.INSTANCE);
    query.addScalar("adminPass", StringType.INSTANCE);
    query.addScalar("operatorName", StringType.INSTANCE);
    query.addScalar("operatorPass", StringType.INSTANCE);
    query.addScalar("viewerName", StringType.INSTANCE);
    query.addScalar("viewerPass", StringType.INSTANCE);
    query.setResultTransformer(Transformers.aliasToBean(CamUserVO.class));
    query.setCacheable(true);
    CamUserVO camUserVO = ((List<CamUserVO>) query.list()).get(0);
    return camUserVO;
}

From source file:controlers.AdminControler.java

public AdminControler() {
    Session session = null;/* w ww  .  ja  va2 s.  c  om*/
    Transaction tx = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        String sql = "SELECT R.idRez, K.username, F.naziv AS nazivFestivala, R.paket, D.redniBroj AS kojiDan, R.brojUlaznica, R.vremeRez, F.cenaPaket, F.cenaDan "
                + "FROM `rezervacija` R, `korisnik` K, `festival` F, `Dan` D "
                + "WHERE R.username = K.username " + "AND R.idFest = F.idFest " + "AND R.idDan = D.idDan "
                + "AND R.status = 'rezervacija' " + "AND R.vremeRez < :current_date " + "GROUP BY R.idRez "
                + "ORDER BY R.vremeRez DESC;";

        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter("current_date", new Date());
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        sveRezervacije = query.list();

        Criteria cr = session.createCriteria(Festival.class);
        cr.add(Restrictions.ne("status", "prosao"));
        cr.add(Restrictions.ne("status", "otkazan"));
        cr.addOrder(Order.asc("datumVremeDo"));
        List result = cr.list();

        aktuelniFestivali = new HashMap<String, Long>();
        for (Object obj : result) {
            Festival festival = (Festival) obj;
            aktuelniFestivali.put(festival.getNaziv(), festival.getIdFest());
        }

    } catch (Exception ex) {
        if (tx != null)
            tx.rollback();
        ex.printStackTrace();
    } finally {
        if (tx != null)
            tx.commit();
        session.close();
    }
}

From source file:controlers.AdminControler.java

public void buyReserve(Long idRes) {

    Session session = null;/*from   w  ww . j a  v a2 s.  c om*/
    Transaction tx = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        Criteria cr = session.createCriteria(Rezervacija.class);
        cr.add(Restrictions.eq("idRez", idRes));
        cr.setMaxResults(1);
        List result = cr.list();
        Rezervacija rezervacija = (Rezervacija) result.get(0);

        rezervacija.setStatus("kupljeno");

        session.save(rezervacija);

        tx.commit();
        session.close();

        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();

        String sql = "SELECT R.idRez, K.username, F.naziv AS nazivFestivala, R.paket, D.redniBroj AS kojiDan, R.brojUlaznica, R.vremeRez, F.cenaPaket, F.cenaDan "
                + "FROM `rezervacija` R, `korisnik` K, `festival` F, `Dan` D "
                + "WHERE R.username = K.username " + "AND R.idFest = F.idFest " + "AND R.idDan = D.idDan "
                + "AND R.status = 'rezervacija' " + "AND R.vremeRez < :current_date " + "GROUP BY R.idRez "
                + "ORDER BY R.vremeRez DESC;";
        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter("current_date", new Date());
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        sveRezervacije = query.list();

    } catch (Exception ex) {
        if (tx != null)
            tx.rollback();
        ex.printStackTrace();
    } finally {
        if (tx != null)
            tx.commit();
        session.close();
    }

}