Example usage for org.hibernate Query setParameterList

List of usage examples for org.hibernate Query setParameterList

Introduction

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

Prototype

Query<R> setParameterList(int position, Object[] values);

Source Link

Usage

From source file:com.zutubi.pulse.master.model.persistence.hibernate.HibernateChangelistDao.java

License:Apache License

public List<PersistentChangelist> findLatestByProjects(Project[] projects, final int max) {
    final Long[] projectIds = new Long[projects.length];
    for (int i = 0; i < projects.length; i++) {
        projectIds[i] = projects[i].getId();
    }/*from   w  w w  .j  a  v  a2  s. co m*/

    return findUnique(new ChangelistQuery() {
        public Query createQuery(Session session) {
            Query queryObject = session.createQuery(
                    "from PersistentChangelist model where model.projectId in (:projectIds) order by model.time desc, model.id desc");
            queryObject.setParameterList("projectIds", projectIds);
            return queryObject;
        }
    }, max);
}

From source file:dao.LocalityDao.java

public List<Locality> getLocs(Long locIds[]) {
    String hql = "from Locality where id in (:locIds)";
    Query query = getCurrentSession().createQuery(hql);
    query.setParameterList("locIds", locIds);
    return query.list();
}

From source file:daoImpl.GroupsDAOImpl.java

@Override
public List<SearchResult> findNearestGroups(float lati, float longi, float maxDistance) {
    Session session = sFac.openSession();
    SQLQuery getDistances = session.createSQLQuery("Select groups.group_id, "
            + "(6371 * acos( cos( radians(:lati )) * cos( radians( groups.latitude ) ) * cos( radians( groups.longitude ) - radians(:longi) ) + sin( radians(:lati) ) * sin( radians( groups.latitude ) ) ) ) as distance "
            + "from groups having distance < :maxDistance order by distance;");
    getDistances.setParameter("lati", lati);
    getDistances.setParameter("longi", longi);
    getDistances.setParameter("maxDistance", maxDistance);
    getDistances.addScalar("group_id", StandardBasicTypes.INTEGER);
    getDistances.addScalar("distance", StandardBasicTypes.FLOAT);

    List<Object[]> closestGroups = getDistances.list();

    if (closestGroups.isEmpty())
        return new ArrayList<>();

    ArrayList groupsToGet = new ArrayList<>();

    for (Object[] r : closestGroups) {
        int i = (int) r[0];
        groupsToGet.add(i);//  www.  j av a 2  s.  c  o  m
    }

    Query getGroups = session.createQuery(
            "from Groups G left join fetch G.tagses left join fetch G.usersGroupses where G.groupId in (:ids)");
    getGroups.setParameterList("ids", groupsToGet);
    getGroups.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Groups> groupsFound = getGroups.list();

    ArrayList<SearchResult> results = new ArrayList<>();

    for (int i = 0; i < groupsFound.size(); i++) {
        SearchResult s = new SearchResult(groupsFound.get(i), (float) (closestGroups.get(i)[1]));
        results.add(s);
    }
    session.close();
    return results;
}

From source file:daoImpl.GroupsDAOImpl.java

@Override
public List<SearchResult> findNearestGroupsByName(float lati, float longi, float maxDistance,
        String searchTerm) {//from  w ww .ja va 2  s  .  com
    Session session = sFac.openSession();
    SQLQuery getDistances = session.createSQLQuery("Select groups.group_id, "
            + "(6371 * acos( cos( radians(:lati )) * cos( radians( groups.latitude ) ) * cos( radians( groups.longitude ) - radians(:longi) ) + sin( radians(:lati) ) * sin( radians( groups.latitude ) ) ) ) as distance "
            + "from groups where groups.groupname like concat('%', :name , '%') having distance < :maxDistance order by distance;");
    getDistances.setParameter("lati", lati);
    getDistances.setParameter("longi", longi);
    getDistances.setParameter("maxDistance", maxDistance);
    getDistances.setParameter("name", searchTerm);
    getDistances.addScalar("group_id", StandardBasicTypes.INTEGER);
    getDistances.addScalar("distance", StandardBasicTypes.FLOAT);
    List<Object[]> closestGroups = getDistances.list();

    if (closestGroups.isEmpty())
        return new ArrayList<>();

    ArrayList groupsToGet = new ArrayList<>();

    for (Object[] r : closestGroups) {
        int i = (int) r[0];
        groupsToGet.add(i);
    }

    Query getGroups = session.createQuery(
            "from Groups G left join fetch G.tagses left join fetch G.usersGroupses where G.groupId in (:ids)");
    getGroups.setParameterList("ids", groupsToGet);
    getGroups.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Groups> groupsFound = getGroups.list();

    ArrayList<SearchResult> results = new ArrayList<>();

    for (int i = 0; i < groupsFound.size(); i++) {
        SearchResult s = new SearchResult(groupsFound.get(i), (float) (closestGroups.get(i)[1]));
        results.add(s);
    }
    session.close();
    return results;
}

From source file:daoImpl.GroupsDAOImpl.java

@Override
public List<SearchResult> findNearestGroupsByDesc(float lati, float longi, float maxDistance,
        String description) {/*  w w w. jav a  2  s  . c  o m*/
    Session session = sFac.openSession();
    SQLQuery getDistances = session.createSQLQuery("Select groups.group_id, "
            + "(6371 * acos( cos( radians(:lati )) * cos( radians( groups.latitude ) ) * cos( radians( groups.longitude ) - radians(:longi) ) + sin( radians(:lati) ) * sin( radians( groups.latitude ) ) ) ) as distance "
            + "from groups where groups.description like concat('%', :name , '%') having distance < :maxDistance order by distance;");
    getDistances.setParameter("lati", lati);
    getDistances.setParameter("longi", longi);
    getDistances.setParameter("maxDistance", maxDistance);
    getDistances.setParameter("name", description);
    getDistances.addScalar("group_id", StandardBasicTypes.INTEGER);
    getDistances.addScalar("distance", StandardBasicTypes.FLOAT);
    List<Object[]> closestGroups = getDistances.list();

    if (closestGroups.isEmpty())
        return new ArrayList<>();

    ArrayList groupsToGet = new ArrayList<>();

    for (Object[] r : closestGroups) {
        int i = (int) r[0];
        groupsToGet.add(i);
    }

    Query getGroups = session.createQuery(
            "from Groups G left join fetch G.tagses left join fetch G.usersGroupses where G.groupId in (:ids)");
    getGroups.setParameterList("ids", groupsToGet);
    getGroups.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Groups> groupsFound = getGroups.list();

    ArrayList<SearchResult> results = new ArrayList<>();

    for (int i = 0; i < groupsFound.size(); i++) {
        SearchResult s = new SearchResult(groupsFound.get(i), (float) (closestGroups.get(i)[1]));
        results.add(s);
    }
    session.close();
    return results;
}

From source file:daoImpl.GroupsDAOImpl.java

@Override
public List<SearchResult> findNearestGroupsByTag(float latitude, float longitude, float maxDistance,
        String tag) {// w w w. j a v a2s .  co  m
    Session session = sFac.openSession();
    SQLQuery query = session.createSQLQuery(
            "Select groups.group_id, (6371 * acos( cos( radians(:lati )) * cos( radians( groups.latitude ) ) * cos( radians( groups.longitude ) - radians(:longi) ) + sin( radians(:lati) ) * sin( radians( groups.latitude ) ) ) ) as distance from groups join groups_tags on (groups.group_id=groups_tags.group_id_fk) join tags on (groups_tags.tag_id_fk=tags.tag_id) where tags.tag_name = :tagname having distance < :dist order by distance;");
    query.setParameter("lati", latitude);
    query.setParameter("longi", longitude);
    query.setParameter("tagname", tag);
    query.setParameter("dist", maxDistance);
    query.addScalar("group_id", StandardBasicTypes.INTEGER);
    query.addScalar("group_id", StandardBasicTypes.FLOAT);

    List<Object[]> closestGroups = query.list();

    if (closestGroups.isEmpty())
        return new ArrayList<>();

    ArrayList groupsToGet = new ArrayList<>();

    for (Object[] r : closestGroups) {
        int i = (int) r[0];
        groupsToGet.add(i);
    }

    Query getGroups = session.createQuery(
            "from Groups G left join fetch G.tagses left join fetch G.usersGroupses where G.groupId in (:ids)");
    getGroups.setParameterList("ids", groupsToGet);
    getGroups.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    List<Groups> groupsFound = getGroups.list();

    ArrayList<SearchResult> results = new ArrayList<>();

    for (int i = 0; i < groupsFound.size(); i++) {
        SearchResult s = new SearchResult(groupsFound.get(i), (float) (closestGroups.get(i)[1]));
        results.add(s);
    }
    session.close();
    return results;
}

From source file:DAOS.DailyHistoryReportImp.java

public ArrayList<ReportsInterface> getDailyHistory(int garageId) {

    ArrayList<ReportsInterface> result = null;
    try {//  w  w w  .  j a va2 s.c  om

        Query q = dailyHistoryReportSession.createQuery("from DailyHistory where slotId in :slotid");
        q.setParameterList("slotid", getGarageSlots(garageId));
        result = (ArrayList<ReportsInterface>) q.list();

    } catch (Exception ex) {
        ex.printStackTrace();

    }

    return result;
}

From source file:DAOS.DailyHistoryReportImp.java

public ArrayList<ReportsInterface> getConsiceDailyHistory(int garageId) {

    Garage garage = GarageImp.getInstance().getGarage(garageId);
    ArrayList<ReportsInterface> result = new ArrayList<>();
    ;/*from  www  . j  a  va  2s  . c  om*/
    try {

        Query q = dailyHistoryReportSession.createQuery(
                "select  recordDate  ,  sum(hours) ,sum( income) from DailyHistory where slotId in :slotid group by recordDate order by recordDate ");
        q.setParameterList("slotid", garage.getGarageStatus());
        ArrayList<Object[]> res = (ArrayList<Object[]>) q.list();

        for (Object[] obj : res) {
            result.add(new DailyHistory((Date) obj[0], Double.valueOf(obj[1].toString()),
                    Double.valueOf(obj[2].toString())));

        }

    } catch (Exception ex) {
        ex.printStackTrace();

    }

    return result;
}

From source file:DAOS.GarageSlotDoorsImp.java

public GObjects.Garage generateGarageObject(int garageId) {

    GObjects.Garage garage = null;//from  w ww  .j  av  a 2s .c  o  m
    try {

        Garage myGarage = GarageImp.getInstance().getGarage(garageId);
        Collection<GarageStatus> slots = myGarage.getGarageStatus();
        Collection<GarageDoors> doors = myGarage.getGarageDoors();
        Query q = garageSession
                .createQuery("from GarageSlotsDoors where slotId  in  :slots and doorId in :doors");
        q.setParameterList("slots", slots);
        q.setParameterList("doors", doors);
        Collection<GarageSlotsDoors> garageSlotsDoors = q.list();

        garage = new GObjects.Garage(garageId);

        ArrayList<GObjects.Door> tempDoors = new ArrayList<GObjects.Door>();
        ArrayList<GObjects.Slot> tempSlot = new ArrayList<GObjects.Slot>();
        Door currentDoor;
        Slot currentSlot;

        for (GarageSlotsDoors garageSlotsDoor : garageSlotsDoors) {
            GarageDoors g = garageSlotsDoor.getDoorId();
            GarageStatus s = garageSlotsDoor.getSlotId();

            if (!contains(tempDoors, garageSlotsDoor.getDoorId().getDoorName())) {
                currentDoor = new Door(g.getDoorId(), g.getDoorName(), g.getX(), g.getY(), g.getLat(),
                        g.getLon());
                tempDoors.add(currentDoor);

            } else {
                currentDoor = getElement(tempDoors, g.getDoorName());
            }
            if (!contains(tempSlot, garageSlotsDoor.getSlotId().getSlotName())) {
                currentSlot = new Slot(s.getSlotName(), s.getX(), s.getY(),
                        preparePathArray(garageSlotsDoor.getPoints()), s.getTotalDailyConsumedHours(),
                        s.getTotalMonthlyConsumedHours(), s.getTotalYearlyConsumedHours());
                tempSlot.add(currentSlot);

            } else {
                currentSlot = getElement(tempSlot, s.getSlotName());
            }

            currentDoor.getSlots().add(currentSlot);
            garage.setDoors(tempDoors);
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        garageSession.getTransaction().rollback();

    }
    return garage;
}

From source file:DAOS.GarageSlotDoorsImp.java

private void deletingGarageData(int garageID) {
    Transaction beginTransaction = null;
    Query query = null;
    try {/*from  ww  w. j a  va  2s.co  m*/

        beginTransaction = garageSession.beginTransaction();
        Garage garage1 = (Garage) garageSession.get(Garage.class, garageID);
        if (garage1 != null) {
            if (garage1.getGarageStatus().size() > 0) {
                query = garageSession.createQuery("delete from GarageSlotsDoors where slotId in (:slots)");
                query.setParameterList("slots", garage1.getGarageStatus());

            }
            for (GarageStatus slot : garage1.getGarageStatus()) {

                garageSession.delete(slot);
            }

            for (GarageDoors door : garage1.getGarageDoors()) {

                garageSession.delete(door);
            }
            if (query != null) {
                query.executeUpdate();
            }
        }
    } catch (Exception ex) {

        ex.printStackTrace();
    } finally {
        if (beginTransaction != null) {
            beginTransaction.commit();

        }

    }

}