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:controlers.UserControler.java

public String detailFestival(Long festivalId) {
    String resultPage = "festival";

    Session session = null;/*from ww  w .j ava2  s  .c om*/
    Transaction tx = null;
    try {
        session = HibernateUtil.getSessionFactory().openSession();
        tx = session.beginTransaction();
        Criteria cr = session.createCriteria(Festival.class);
        cr.add(Restrictions.eq("idFest", festivalId));
        List result = cr.list();
        if (result.size() > 0) {
            currentFestival = (Festival) result.get(0);

            currentFestival.setBrojPregleda(currentFestival.getBrojPregleda() + 1);

            session.save(currentFestival);

            String sql = "SELECT I.naziv, I.vremeOd, I.vremeDo, D.redniBroj FROM izvodjac I, dan D WHERE I.idFest = :festival_id AND D.idFest = :festival_id AND I.idDan = D.idDan ORDER BY I.vremeOd";
            SQLQuery query = session.createSQLQuery(sql);
            query.setParameter("festival_id", festivalId);
            query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
            izvodjaciFestivala = query.list();

            dani = new HashMap<String, String>();
            izvodjaciFestivala.forEach((Map m) -> {
                dani.put(m.get("redniBroj").toString(), m.get("redniBroj").toString());
            });

            cr = session.createCriteria(Media.class);
            cr.add(Restrictions.eq("festival", currentFestival));
            cr.add(Restrictions.eq("slikaVideo", "slika"));
            result = cr.list();
            if (result.size() > 0)
                currentFestivalMainImg = ((Media) result.get(0)).getUrl();
            else
                currentFestivalMainImg = null;

            cr = session.createCriteria(Link.class);
            cr.add(Restrictions.eq("festival", currentFestival));
            result = cr.list();
            if (result.size() > 0)
                linkoviFestivala = (List<Link>) result;
            else
                linkoviFestivala = new ArrayList<Link>();
        } else {
            porukaZaPretragu = "Dolo je do greke, molimo Vas, pokuajte malo kasnije.";
            resultPage = "index";
        }
    } catch (Exception ex) {
        if (tx != null)
            tx.rollback();
        ex.printStackTrace();
    } finally {
        if (tx != null)
            tx.commit();
        session.close();
    }

    return resultPage;
}

From source file:controlers.UserControler.java

public String reservations() {

    Session session = null;/*w ww.j  a v  a 2s  . c  o  m*/
    Transaction tx = null;

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

        String sql = "SELECT F.status AS statusFestivala, R.idRez, F.naziv, R.vremeRez, F.datumVremeDo, R.paket, R.brojUlaznica, F.cenaPaket, F.cenaDan, R.status FROM rezervacija R, festival F WHERE R.username = :username AND R.idFest = F.idFest ORDER BY R.vremeRez DESC;";
        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter("username", LogedInKorisnik.korisnik.getUsername());
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        rezervacije = query.list();

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

    return "reservations";
}

From source file:controlers.UserControler.java

public String cancelReservation(Long rezId) {

    Session session = null;/*from w  w  w  .ja va  2s .c  o  m*/
    Transaction tx = null;

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

        Criteria cr = session.createCriteria(Rezervacija.class);
        cr.add(Restrictions.eq("idRez", rezId));
        List result = cr.list();
        Rezervacija rezForDelete = (Rezervacija) result.get(0);

        boolean isPaket = rezForDelete.getPaket();
        int numOfUlaznica = rezForDelete.getBrojUlaznica().intValue();

        Criteria cr2 = session.createCriteria(Dan.class);
        cr2.add(Restrictions.eq("festival", rezForDelete.getFestival()));
        List daysOfFestival = cr2.list();

        for (Object obj : daysOfFestival) {
            Dan dan = (Dan) obj;
            if (rezForDelete.getPaket() || dan.getRedniBroj() == rezForDelete.getDan().getRedniBroj()) {
                dan.setBrojUlaznica(dan.getBrojUlaznica() + rezForDelete.getBrojUlaznica());
                session.save(dan);
                //tx.commit();                        
            }
        }

        rezForDelete.setStatus("otkazana");

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

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

        String sql = "SELECT F.status AS statusFestivala, R.idRez, F.naziv, R.vremeRez, F.datumVremeDo, R.paket, R.brojUlaznica, F.cenaPaket, F.cenaDan, R.status FROM rezervacija R, festival F WHERE R.username = :username AND R.idFest = F.idFest ORDER BY R.vremeRez DESC;";
        SQLQuery query = session.createSQLQuery(sql);
        query.setParameter("username", LogedInKorisnik.korisnik.getUsername());
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        rezervacije = query.list();

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

    currentDate = new Date();
    return "reservations";
}

From source file:dao.DrugGraphDataDao.java

public List<DrugGraphData> getPatientAdmissionYearMonth() {
    List<DrugGraphData> objects = null;
    SQLQuery sqlq = null;

    try {/* w w w  . j av a 2  s.  c o m*/
        tx = session.beginTransaction();
        sqlq = session.createSQLQuery(
                "select c.name as CompoundName,count(*) as CompoundUsedNumber From Drug_has_Compound dhc join Compound c on\n"
                        + " c.idCompound=dhc.idCompound Group BY dhc.idCompound");
        objects = sqlq.setResultTransformer(Transformers.aliasToBean(DrugGraphData.class)).list();
        //            System.out.println(objects.size());
        tx.commit();
    } catch (HibernateException e) {
        tx.rollback();
    }
    return objects;
}

From source file:dao.JoinSeletedCompoundDao.java

public List<JoinSelectedCompound> findCompoundByDrugNumber(String str) {
    List<JoinSelectedCompound> objects = null;
    SQLQuery sqlq = null;

    try {//from  w  ww. j a va  2  s .  c o m
        tx = session.beginTransaction();
        sqlq = session.createSQLQuery("call selectedCompoundJionCompound(:str)");
        sqlq.setString("str", str);
        objects = sqlq.setResultTransformer(Transformers.aliasToBean(JoinSelectedCompound.class)).list();
        tx.commit();
    } catch (HibernateException e) {
        tx.rollback();
    }
    return objects;
}

From source file:dao.PatientGraphDataDao.java

public List<PatientGraphData> getPatientAdmissionYearMonth() {
    List<PatientGraphData> objects = null;
    SQLQuery sqlq = null;

    try {/*  w  w  w  .  j  a v a2s.  co m*/
        tx = session.beginTransaction();
        sqlq = session.createSQLQuery(
                "SELECT extract(year from registration_date) as year, extract(month from registration_date) as month, \n"
                        + "COUNT(*) as patientNum  FROM patients GROUP BY extract(year from registration_date), extract(month from registration_date) \n"
                        + "ORDER BY Year,MONTH");
        objects = sqlq.setResultTransformer(Transformers.aliasToBean(PatientGraphData.class)).list();
        //            System.out.println(objects.size());
        tx.commit();
    } catch (HibernateException e) {
        tx.rollback();
    }
    return objects;
}

From source file:data.dao.SubModelDao.java

public HashMap<Long, String> getGenIdUrlMap() {
    HashMap<Long, String> res = new HashMap();
    String sql = "select id,url from car_model_generation";
    SQLQuery query = currentSession().createSQLQuery(sql);
    query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
    List data = query.list();/*  w  ww.  ja  v  a  2 s  .  com*/

    for (Object object : data) {
        Map row = (Map) object;
        res.put(Long.valueOf((int) row.get("id")), StringAdapter.getString(row.get("url")));
    }
    return res;
}

From source file:edu.uiowa.icts.bluebutton.dao.LabTestHome.java

License:Apache License

private List<LoincCode> labTestQuery(String sex, Double age, String loincCodeCsvList) {
    List<LoincCode> list = new ArrayList<LoincCode>();
    if (sex != null && sex.length() > 0 && age != null) {
        sex = sex.substring(0, 1).toUpperCase();

        String sql = "select loinc_code as \"loinc_code\", min_normal as \"min_normal\", max_normal as \"max_normal\""
                + "   from bluebutton.lab_test lt, bluebutton.lab_test_range ltr "
                + "   where lt.lab_test_id = ltr.lab_test_id " + "   and ltr.sex like :sex "
                + "   and :age >= ltr.min_age_years  " + "   and :age < ltr.max_age_years "
                + "   and loinc_code IS NOT NULL ";
        if (loincCodeCsvList != null) {
            sql += "and loinc_code in (:loincCodeCsvList) ";
        }//from ww w  .j  av  a  2 s .c  o m
        SQLQuery query = this.sessionFactory.getCurrentSession().createSQLQuery(sql);
        query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
        query.setString("sex", "%" + sex + "%");
        query.setDouble("age", age);
        if (loincCodeCsvList != null) {
            query.setParameterList("loincCodeCsvList", loincCodeCsvList.split(","));
        }
        List data = query.list();

        for (Object object : data) {
            Map row = (Map) object;
            LoincCode lc = new LoincCode(row.get("loinc_code").toString(),
                    new Double((double) row.get("min_normal")), new Double((double) row.get("max_normal")));
            list.add(lc);
        }
    }
    return list;
}

From source file:es.emergya.bbdd.dao.RecursoHome.java

License:Open Source License

@Transactional(readOnly = true, rollbackFor = Throwable.class, propagation = Propagation.REQUIRED)
public RecursoBean getByDispositivoSQL(Integer issi) {

    SQLQuery query = getSession().createSQLQuery("select r.x_recurso as "
            + "id, r.identificador as identificador, r.habilitado as habilitado, "
            + "r.tipo as \"tipoRecurso\", s.nombre as "
            + "subflota, r.dispositivo as dispositivo from recursos as r join flotas s on r.flota_x_flota = s.x_flota "
            + "where r.dispositivo = :DISPOSITIVO");
    query.setInteger("DISPOSITIVO", issi);
    query.setResultTransformer(Transformers.aliasToBean(RecursoBean.class));
    RecursoBean recurso = (RecursoBean) query.uniqueResult();

    return recurso;

}

From source file:gemlite.core.internal.support.jpa.files.dao.GmBatchDaoImpl.java

License:Apache License

@Override
public List<Map> queryJobExecutions(String status) {
    String where = "";
    if (!StringUtils.isEmpty(status)) {
        where = " where e.status='" + status + "'";
    }//from www  .j a va2  s .  c  om
    String getjobs = "select e.job_execution_id as job_execution_id, e.start_time as start_time, e.end_time, e.status, e.exit_code, e.exit_message, e.create_time, e.last_updated, e.version, i.job_instance_id, i.job_name from batch_job_execution e join batch_job_instance i on e.job_instance_id=i.job_instance_id "
            + where + " order by job_execution_id desc";
    Query query = em.createNativeQuery(getjobs);
    SQLQuery nativeQuery = query.unwrap(SQLQuery.class);
    nativeQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
    List<Map> list = null;
    try {
        list = nativeQuery.list();
    } catch (Exception e) {
        LogUtil.getCoreLog().warn("SQL {} Error {}", getjobs, e.getMessage());
    }
    //?
    List<Map> newlist = new ArrayList<Map>();
    if (list != null) {
        for (Map map : list) {
            //duration
            Timestamp t1 = (Timestamp) map.get("START_TIME");
            Timestamp t2 = (Timestamp) map.get("END_TIME");
            String duration = "";
            if (t2 != null)
                duration = format(new Date(t2.getTime() - t1.getTime()), "HH:mm:ss");

            Map newmap = new HashMap<String, Object>();
            newmap.put("duration", duration);

            Iterator<Entry<String, Object>> it = map.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> entry = it.next();
                newmap.put(entry.getKey().toLowerCase(), entry.getValue());
            }
            newlist.add(newmap);
        }
    }

    return newlist;
}