Example usage for org.hibernate Query setLong

List of usage examples for org.hibernate Query setLong

Introduction

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

Prototype

@Deprecated
@SuppressWarnings("unchecked")
default Query<R> setLong(String name, long val) 

Source Link

Document

Bind a named long-valued parameter.

Usage

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

@Override
public CargoMessage getFirstLineMessage(int start, int end, Long userId, Long updateTime) {
    String hql = "from CargoMessage message where message.updateTime>=? and message.userId=? and message.start=? and message.end=?";
    Session session = null;//w w  w  .  ja  v a2 s.  com
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(updateTime);
        session = sessionFactory.openSession(inter);
        Query query = session.createQuery(hql);
        query.setLong(0, TimeUtils.getStartOfDay(updateTime));
        query.setLong(1, userId);
        query.setInteger(2, start);
        query.setInteger(3, end);
        query.setFirstResult(0);
        query.setMaxResults(1);
        CargoMessage message = (CargoMessage) query.uniqueResult();
        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

@Override
public int refreshCargoMessages() {

    LogUtil.i(this.getClass().getSimpleName(), "refreshCargoMessages");
    Session session = null;//from  w w  w .  j  a v a  2s  . c  om
    Transaction tx = null;
    int size = 0;
    long twenty_m_ago = System.currentTimeMillis() - 1200000;
    long now = System.currentTimeMillis();
    String hql = "update CargoMessage message set message.updateTime=?-(1200000-MOD(message.updateTime, 1200000)) where message.updateTime<=? and message.updateTime>? and message.type=1";
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(System.currentTimeMillis());
        session = sessionFactory.openSession(inter);
        tx = session.beginTransaction();
        tx.setTimeout(Constant.TRANSCTION_TIMEOUT);
        Query query = session.createQuery(hql);
        query.setLong(0, now);
        query.setLong(1, twenty_m_ago);
        query.setLong(2, TimeUtils.getStartOfDay(now));
        size = query.executeUpdate();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    return size;
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

@Override
public int getUserMessageCountByTime(long startOfDay, long endOfDay, Long userId) {
    Session session = null;// www  . j  a v  a  2 s.c  o  m
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(startOfDay + 1);
        session = sessionFactory.openSession(inter);
        int count = 0;
        String hql = "select count(*) from CargoMessage msg where msg.updateTime<? and msg.updateTime>? and (msg.type=1 or msg.type=-1 or msg.type=4) and msg.userId=?";
        Query query = session.createQuery(hql);
        query.setLong(0, endOfDay);
        query.setLong(1, startOfDay);
        query.setLong(2, userId);
        count = ((Long) query.uniqueResult()).intValue();
        return count;
    } catch (Exception e) {
        e.printStackTrace();
        return 0;
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

@Override
public List<Object> searchCargoByClerk(TelephoneRequest req) {
    Session session = null;/*from   w  ww.  ja  v a  2s.c o m*/
    long startOfDay = TimeUtils.getStartOfDay(System.currentTimeMillis()) + 1;
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(startOfDay + 1);
        session = sessionFactory.openSession(inter);
        String hql = "select new com.consult.app.response.admin.ClerkMessageItem(message) "
                + "from CargoMessage message "
                + "where message.telephone=? and message.updateTime>? and (message.type=1 or message.type=-1 or message.type=2 or message.type=-2)";
        Query query = session.createQuery(hql);
        query.setString(0, req.getTelephone());
        query.setLong(1, startOfDay);
        query.setFirstResult(0);
        query.setMaxResults(100);
        List<Object> list = query.list();
        return list;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    return null;
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

@Override
public List<Object> searchAllCargoByAdmin(TelephoneBeforeAfterCountRequest req) {
    Session session = null;//from  w  w  w .  ja v a2s.  c om
    List<Object> list = null;
    //      long startOfDay = TimeUtils.getStartOfDay(System.currentTimeMillis()) + 1;
    try {
        CargoMessageInterceptor interBefore = new CargoMessageInterceptor(req.getBefore());
        int beforeIndex = interBefore.getTableIndex();

        CargoMessageInterceptor interAfter = new CargoMessageInterceptor(req.getAfter());
        int afterIndex = interAfter.getTableIndex();

        if (beforeIndex != afterIndex) {
            LogUtil.i("searchAllCargoByAdmin", " beforeIndex != afterIndex ");
            return list;
        }
        session = sessionFactory.openSession(interBefore);
        String hql = "select new com.consult.app.response.admin.ClerkMessageItem(message) "
                + "from CargoMessage message "
                + "where message.telephone=? and message.createTime>? and message.createTime<? order by "
                + " message.createTime desc";
        Query query = session.createQuery(hql);
        query.setString(0, String.valueOf(req.getTelephone()));
        query.setLong(1, req.getBefore());
        query.setLong(2, req.getAfter());
        query.setMaxResults(req.getCount());
        list = query.list();
        return list;
    } 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 int deleteClerkCargo(IdRequest req) {

    Session session = null;//from   w ww  .j a  v  a2 s  .c  o m
    Transaction tx = null;
    long now = System.currentTimeMillis();
    String hql = "update CargoMessage message set message.type=-message.type where message.updateTime>? and message.type>0 and message.type<4 and message.messageId=?";
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(System.currentTimeMillis());
        session = sessionFactory.openSession(inter);
        tx = session.beginTransaction();
        tx.setTimeout(Constant.TRANSCTION_TIMEOUT);
        Query query = session.createQuery(hql);
        query.setLong(0, TimeUtils.getStartOfDay(now));
        query.setLong(1, req.getId());
        query.executeUpdate();
        tx.commit();
    } catch (Exception e) {
        e.printStackTrace();
        if (tx != null) {
            tx.rollback();
        }
        return Cookie.RESPONSE_SERVER_QUERY_ERROR;
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    return Cookie.RESPONSE_SUCCESS;
}

From source file:com.consult.app.dao.impl.CargoMessageDaoImpl.java

@Override
public List<Object> getCargosByTel(GetCargosRequest req) {
    Session session = null;//from   w  w w.j  a  v a  2 s .c o  m
    long now = System.currentTimeMillis();
    String hql = "select new com.consult.app.response.admin.GetCargosItem(message.messageId,message.userId,message.start,message.end,message.telephone,"
            + "message.contact,message.weight,message.capacity,message.truckLength,message.truckType,message.type,message.description,message.modifyTime,"
            + "message.updateTime,message.companyName,message.companyAddress,message.landlines,message.charges,message.cargoType,message.truckLengthSet) from CargoMessage message "
            + "where message.telephone=? and message.modifyTime > ? and message.modifyTime < ? and (message.type=1 or message.type=-1 or message.type=4)";
    if (req.getCity() > 0) {
        hql += " and message.start = ?";
    }
    hql += " order by message.modifyTime desc";
    try {
        CargoMessageInterceptor inter = new CargoMessageInterceptor(System.currentTimeMillis());
        session = sessionReadFactory.openSession(inter);
        Query query = session.createQuery(hql);
        query.setLong(0, req.getTelephone());
        query.setLong(1, TimeUtils.getStartOfDay(req.getUpdateTime()));
        query.setLong(2, TimeUtils.getEndOfDay(req.getUpdateTime()));
        if (req.getCity() > 0) {
            query.setInteger(3, req.getCity());
        }
        List<Object> list = query.list();
        return list;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null) {
            session.close();
            session = null;
        }
    }
    return null;
}

From source file:com.consult.app.dao.impl.UserDaoImpl.java

public User findById(Long userId) {
    try {/*  w  w w . j  av a 2  s.c om*/
        User user = JUserCache.getInstance().findUserByUserId(userId);
        if (user == null) {
            Query query = sessionFactory.getCurrentSession().createQuery("from User user where user.userId=?");
            query.setLong(0, userId);
            user = (User) query.uniqueResult();
            if (user != null) {
                JUserCache.getInstance().saveUser(userId, user);
            }
        }
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.consult.app.dao.impl.UserDaoImpl.java

public User findByIdNewSession(Long userId) {
    try {/*ww  w  . j  a va2 s .  com*/
        User user = JUserCache.getInstance().findUserByUserId(userId);
        if (user == null) {
            Session session = null;
            try {
                session = sessionFactory.openSession();
                Query query = session.createQuery("from User user where user.userId=?");
                query.setLong(0, userId);
                user = (User) query.uniqueResult();
                if (user != null) {
                    JUserCache.getInstance().saveUser(userId, user);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                if (session != null) {
                    session.close();
                    session = null;
                }
            }
        }
        return user;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.consult.app.dao.impl.UserDaoImpl.java

@Override
public User findByTelephone(Long telephone) {
    try {/*from www.  java  2 s .  c  o m*/
        Query query = sessionFactory.getCurrentSession().createQuery("from User user where user.telephone=?");
        query.setLong(0, telephone);
        return (User) query.uniqueResult();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}