List of usage examples for org.hibernate Query setLong
@Deprecated @SuppressWarnings("unchecked") default Query<R> setLong(String name, long val)
From source file:com.connexience.server.model.datasets.queries.temporal.enactors.AbsoluteTimeRangeQueryEnactor.java
License:Open Source License
@Override public JSONContainer performQuery() throws ConnexienceException { Session session = null;/*from w ww . j a v a 2 s . c o m*/ try { session = sessionProvider.getSession(); AbsoluteTimeRangeQuery q = (AbsoluteTimeRangeQuery) query; Query hqlQuery = session.createQuery( "from JsonDataRow as obj where obj.itemId=:itemid and obj.collectionTime>=:starttime and obj.collectionTime<=:endtime order by obj.id asc"); hqlQuery.setLong("itemid", item.getId()); hqlQuery.setDate("starttime", q.getStartDate()); hqlQuery.setDate("endtime", q.getEndDate()); List rows = hqlQuery.list(); if (q.getKeyArray() != null && q.getKeyArray().length > 0) { return DatasetsUtils.createResultFromList(rows, q.getKeyArray()); } else { return DatasetsUtils.createResultFromList(rows, null); } } catch (Exception e) { throw new ConnexienceException("Error performing query: " + e.getMessage(), e); } finally { sessionProvider.closeSession(session); } }
From source file:com.connexience.server.model.datasets.queries.temporal.enactors.LatestByAbsoluteTimeQueryEnactor.java
License:Open Source License
@Override public JSONContainer performQuery() throws ConnexienceException { Session session = null;/* w ww. ja v a2 s . co m*/ try { session = sessionProvider.getSession(); LatestByAbsoluteTimeQuery q = (LatestByAbsoluteTimeQuery) query; Query hqlQuery = session.createQuery( "from JsonDataRow as obj where obj.itemId=:itemid and obj.collectionTime>:collectiontime order by obj.id asc"); hqlQuery.setLong("itemid", item.getId()); hqlQuery.setDate("collectiontime", q.getStartDate()); List rows = hqlQuery.list(); if (q.getKeyArray() != null && q.getKeyArray().length > 0) { return DatasetsUtils.createResultFromList(rows, q.getKeyArray()); } else { return DatasetsUtils.createResultFromList(rows, null); } } catch (Exception e) { throw new ConnexienceException("Error performing query: " + e.getMessage(), e); } finally { sessionProvider.closeSession(session); } }
From source file:com.connexience.server.model.datasets.queries.temporal.enactors.LatestByRelativeTimeQueryEnactor.java
License:Open Source License
@Override public JSONContainer performQuery() throws ConnexienceException { Session session = null;/*from ww w . j ava 2s . c o m*/ try { Calendar c = Calendar.getInstance(); LatestByRelativeTimeQuery q = (LatestByRelativeTimeQuery) query; int units = -q.getNumberOfUnits(); // negative units to subtract time if (q.getTimeUnit().equals(DAY)) { c.add(Calendar.DAY_OF_YEAR, units); } else if (q.getTimeUnit().equals(HOUR)) { c.add(Calendar.HOUR_OF_DAY, units); } else if (q.getTimeUnit().equals(MINUTE)) { c.add(Calendar.MINUTE, units); } else if (q.getTimeUnit().equals(SECOND)) { c.add(Calendar.SECOND, units); } else if (q.getTimeUnit().equals(WEEK)) { c.add(Calendar.WEEK_OF_YEAR, units); } else if (q.getTimeUnit().equals(YEAR)) { c.add(Calendar.YEAR, units); } else { c.add(Calendar.DAY_OF_YEAR, units); } session = sessionProvider.getSession(); Query hqlQuery = session.createQuery( "from JsonDataRow as obj where obj.itemId=:itemid and obj.collectionTime>=:collectiontime order by obj.id asc"); hqlQuery.setLong("itemid", item.getId()); hqlQuery.setDate("collectiontime", c.getTime()); List rows = hqlQuery.list(); if (q.getKeyArray() != null && q.getKeyArray().length > 0) { return DatasetsUtils.createResultFromList(rows, q.getKeyArray()); } else { return DatasetsUtils.createResultFromList(rows, null); } } catch (Exception e) { throw new ConnexienceException("Error performing query: " + e.getMessage(), e); } finally { sessionProvider.closeSession(session); } }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
public CargoMessage findById(Long id) { Session session = null;//from ww w . j a v a 2 s. c o m CargoMessage message = null; try { CargoMessageInterceptor inter = new CargoMessageInterceptor(); long triggerTime = System.currentTimeMillis(); // for(int i = 0; i < Constant.SEARCH_TABLE_COUNT; i++) { inter.setShardCriteria(triggerTime); session = sessionFactory.openSession(inter); Query query = session.createQuery("from CargoMessage message where message.messageId=?"); query.setLong(0, id); message = (CargoMessage) query.uniqueResult(); if (message != null) { return message; } if (inter.isFinishSearch(0)) { return null; } triggerTime = inter.getTriggerTime(); // } return message; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (session != null) { session.close(); session = null; } } // try { // Query query = sessionFactory.getCurrentSession().createQuery("from CargoMessage message where message.messageId=?"); // query.setLong(0, id); // return (CargoMessage)query.uniqueResult(); // } catch(Exception e) { // e.printStackTrace(); // return null; // } }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
/** * ???// ww w . j a v a 2s .c o m * */ public CargoMessageItem selectById(Long id) { Session session = null; CargoMessageItem message = null; try { CargoMessageInterceptor inter = new CargoMessageInterceptor(); long triggerTime = System.currentTimeMillis(); inter.setShardCriteria(triggerTime); session = sessionFactory.openSession(inter); Query query = session.createQuery( "select new com.consult.app.response.cargo.CargoMessageItem(message.messageId, message.type, message.weight, message.capacity, message.truckType, message.contact, message.telephone, message.start, message.end, message.description, message.updateTime, message.truckLength, message.companyName, message.companyAddress, message.landlines, message.picture, message.avatarAuthenticate, message.userId, message.licenseAuthenticate, message.charges, message.cargoType, message.score, message.orderCount, message.messageCount, message.truckLengthSet) from CargoMessage message where message.messageId=? and message.updateTime>? and message.type>0 and message.type<?"); query.setLong(0, id); query.setLong(1, TimeUtils.getStartOfDay(triggerTime)); query.setInteger(2, Constant.TYPE_ORDERED); message = (CargoMessageItem) query.uniqueResult(); if (message != null) { return message; } if (inter.isFinishSearch(0)) { return null; } triggerTime = inter.getTriggerTime(); return message; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (session != null) { session.close(); session = null; } } }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
/** * Common Search cargo, used by 274 Version! * This supports multiple truck length/* w w w . j a v a2s. c o m*/ * */ private List<Object> getMultipleTruckLengthSearch(SearchCargoRequest req, City endCity) { Long today = TimeUtils.getStartOfDay(System.currentTimeMillis()); StringBuilder sb = new StringBuilder(String.format(Constant.CARGO_SEARCH_MULTIPLE_TRUCKLENGTH, today)); if (Double.valueOf(req.getTruckLength()) >= 0) { sb.append(" and find_in_set(").append(req.getTruckLength()).append(",message.truck_length_set)"); } if (req.getTruckType() >= 0) { sb.append(" and message.truck_type=").append(req.getTruckType()); } if (req.getWeightRange() > 0) { sb.append(" and message.cargo_weight_range=").append(req.getWeightRange()); } sb.append(" and (message.start=").append(req.getStart()).append(" or message.start_father=") .append(req.getStart()).append(" or message.start_grand=").append(req.getStart()) .append(") and (message.end=").append(endCity.getId()).append(" or message.end_father=") .append(endCity.getId()).append(" or message.end_grand=").append(endCity.getId()).append(" or 0=") .append(endCity.getId()).append(")"); sb.append(" and message.type>=? and message.type<4 order by message.update_time desc"); Long triggerTime = req.getAfter(); if (triggerTime.equals(Long.MAX_VALUE)) { triggerTime = System.currentTimeMillis(); } CargoMessageInterceptor inter = new CargoMessageInterceptor(); int count = req.getCount(); // for(int i = 0; i < Constant.SEARCH_TABLE_COUNT; i++) { Session session = null; try { // Long messageId, int type, // double weight, double capacity, int truckType, // String contact, String telephone, int start, int end, // String description, long updateTime, double truckLength, // String companyName, String companyAddress, String landlines, // String picture, int avatarAuthenticate, Long userId inter.setShardCriteria(System.currentTimeMillis()); session = sessionFactory.openSession(inter); Query query = session.createSQLQuery(sb.toString()).addScalar("messageId", StandardBasicTypes.LONG) .addScalar("type", StandardBasicTypes.INTEGER).addScalar("weight", StandardBasicTypes.DOUBLE) .addScalar("capacity", StandardBasicTypes.DOUBLE) .addScalar("truckType", StandardBasicTypes.INTEGER) .addScalar("contact", StandardBasicTypes.STRING) .addScalar("telephone", StandardBasicTypes.STRING) .addScalar("start", StandardBasicTypes.INTEGER).addScalar("end", StandardBasicTypes.INTEGER) .addScalar("description", StandardBasicTypes.STRING) .addScalar("updateTime", StandardBasicTypes.LONG) .addScalar("truckLength", StandardBasicTypes.DOUBLE) .addScalar("companyName", StandardBasicTypes.STRING) .addScalar("companyAddress", StandardBasicTypes.STRING) .addScalar("landlines", StandardBasicTypes.STRING) .addScalar("picture", StandardBasicTypes.STRING) .addScalar("avatarAuthenticate", StandardBasicTypes.INTEGER) .addScalar("userId", StandardBasicTypes.LONG) .addScalar("licenseAuthenticate", StandardBasicTypes.INTEGER) .addScalar("cargoType", StandardBasicTypes.INTEGER) .addScalar("score", StandardBasicTypes.DOUBLE) .addScalar("orderCount", StandardBasicTypes.INTEGER) .addScalar("charges", StandardBasicTypes.INTEGER) .addScalar("messageCount", StandardBasicTypes.LONG) .addScalar("truckLengthSet", StandardBasicTypes.STRING).setResultTransformer( Transformers.aliasToBean(com.consult.app.response.cargo.CargoMessageItem.class)); query.setLong(0, req.getBefore()); query.setLong(1, triggerTime); query.setInteger(2, Constant.TYPE_NORMAL); query.setFirstResult(0); query.setMaxResults(count); @SuppressWarnings("unchecked") List<Object> tmpList = query.list(); // if(tmpList != null && tmpList.size() > 0) { // list.addAll(tmpList); // if(tmpList.size() >= count) { // tx.commit(); // return list; // } // count -= tmpList.size(); //// min = metaList.get(metaList.size() - 1).getUpdateTime(); // } // if(inter.isFinishSearch(req.getBefore())) { // tx.commit(); // return list; // } // triggerTime = inter.getTriggerTime(); return tmpList; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (session != null) { session.close(); session = null; } } }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
/** * Common Search cargo, used by First Version! * Deprecated!/*from w ww. ja va 2 s . c om*/ */ private List<Object> getAllMessage(SearchCargoRequest req, City endCity) { StringBuilder sb = new StringBuilder(Constant.CARGO_SEARCH); if (req.getTruckLengthRange() > 0) { sb.append(" and message.truckLengthRange=").append(req.getTruckLengthRange()); } if (req.getTruckType() >= 0) { sb.append(" and message.truckType=").append(req.getTruckType()); } if (req.getWeightRange() > 0) { sb.append(" and message.cargoWeightRange=").append(req.getWeightRange()); } sb.append(" and (message.start=").append(req.getStart()).append(" or message.startFather=") .append(req.getStart()).append(") and (message.end=").append(endCity.getId()) .append(" or message.endFather=").append(endCity.getId()).append(" or message.endGrand=") .append(endCity.getId()).append(" or 0=").append(endCity.getId()).append(")"); sb.append(" and message.type>=? and message.type<4 order by message.updateTime desc"); List<Object> list = new ArrayList<Object>(); Long triggerTime = req.getAfter(); if (triggerTime.equals(Long.MAX_VALUE)) { triggerTime = System.currentTimeMillis(); } CargoMessageInterceptor inter = new CargoMessageInterceptor(); int count = req.getCount(); // for(int i = 0; i < Constant.SEARCH_TABLE_COUNT; i++) { Session session = null; try { inter.setShardCriteria(triggerTime); session = sessionFactory.openSession(inter); Query query = session.createQuery(sb.toString()); query.setLong(0, req.getBefore()); query.setLong(1, triggerTime); query.setInteger(2, Constant.TYPE_NORMAL); query.setFirstResult(0); query.setMaxResults(count); @SuppressWarnings("unchecked") List<Object> tmpList = query.list(); if (tmpList != null && tmpList.size() > 0) { list.addAll(tmpList); if (tmpList.size() >= count) { return list; } count -= tmpList.size(); } if (inter.isFinishSearch(req.getBefore())) { return list; } triggerTime = inter.getTriggerTime(); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.close(); session = null; } } // } return list; }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
@Override public Object getMessageInfo(Long updateTime, Long id) { String hql = "select new com.consult.app.response.cargo.CargoMessageItem" + "(message.messageId, message.type, message.weight, message.capacity," + "message.truckType, message.contact, message.telephone, message.start, message.end," + "message.description, message.updateTime, message.truckLength, message.companyName," + "message.companyAddress, message.landlines, message.picture, message.avatarAuthenticate, " + "message.userId, message.licenseAuthenticate, message.charges, message.cargoType, message.score, " + "message.orderCount, message.messageCount, message.truckLengthSet) " + "from CargoMessage message where message.messageId=?"; Session session = null;/*w w w .j av a2 s.co m*/ try { CargoMessageInterceptor inter = new CargoMessageInterceptor(updateTime); session = sessionFactory.openSession(inter); Query query = session.createQuery(hql); query.setLong(0, id); CargoMessageItem message = (CargoMessageItem) query.uniqueResult(); return message; } catch (Exception e) { e.printStackTrace(); return null; } finally { if (session != null) { session.close(); session = null; } } // Query query = sessionFactory.getCurrentSession().createQuery(hql); // query.setLong(0, id); // query.setLong(1, updateTime); // return (CargoMessageItem)query.uniqueResult(); }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
@Override public void doReSend(long start, long end, long now) { String hql = "from CargoMessage message where message.updateTime>? and message.updateTime<? and (message.type=? or message.type=?) order by message.updateTime desc"; if (end <= start) { return;/*from w ww .j a va2s .c om*/ } if (end - start < Integer.MAX_VALUE) { end = start + (end - start) / 4 + RandomNumber.createRandomInt((int) (end - start) * 3 / 4); } Session session = null; List<CargoMessage> list = null; try { CargoMessageInterceptor inter = new CargoMessageInterceptor(end); session = sessionFactory.openSession(inter); Query query = session.createQuery(hql); query.setLong(0, start); query.setLong(1, end); query.setInteger(2, Constant.TYPE_NORMAL); query.setInteger(3, Constant.TYPE_XIWEI); query.setFirstResult(0); query.setMaxResults(20); list = query.list(); } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.close(); session = null; } } if (list != null && !list.isEmpty()) { int i = RandomNumber.createRandomInt(list.size()); CargoMessage message = (CargoMessage) list.get(i); if (message.getType() == Constant.TYPE_ARTIFICIAL) { message.setType(Constant.TYPE_DELETE); update(message); } message.setMessageId(null); message.setUpdateTime(now); message.setModifyTime(now); message.setType(Constant.TYPE_ARTIFICIAL); // message.setUserId(0L); save(message); LogUtil.i(CargoMessage.class.getSimpleName(), "CargoMessage RESEND Success."); updateSubscribeProcess.processAddWithoutPush(message.getStart(), message.getEnd(), message.getEndFather(), message.getEndGrand(), Constant.MESSAGE_TYPE_CARGO, "", now); } else { LogUtil.i(CargoMessage.class.getSimpleName(), "CargoMessage RESEND Nothing."); } }
From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java
@Override public int getTodayCargoCount(int city) { Session session = null;//from w ww . j a va 2 s . c o m long startOfDay = TimeUtils.getStartOfDay(System.currentTimeMillis()); try { CargoMessageInterceptor inter = new CargoMessageInterceptor(startOfDay + 1); session = sessionFactory.openSession(inter); String hql = "select count(distinct message.messageId) from CargoMessage message where message.updateTime>? and (message.start=? or message.startFather=?)"; Query query = session.createQuery(hql); query.setLong(0, startOfDay); query.setInteger(1, city); query.setInteger(2, city); int count = ((Long) query.uniqueResult()).intValue(); return count; } catch (Exception e) { e.printStackTrace(); } finally { if (session != null) { session.close(); session = null; } } return 0; }