public int demSoLuongDonHangTheoTaiKhoan(String maTaiKhoan) { Session session = openSession(); String hql = String.format( "select count(*) from ChiTietHoaDon cthd where cthd.hoaDon.taiKhoan.maTaiKhoan=:maTaiKhoan and cthd.hoaDon.daXoa=:trangThai"); Query query = session.createQuery(hql); query.setString("maTaiKhoan", maTaiKhoan); query.setBoolean("trangThai", false); int result = Integer.parseInt(query.uniqueResult().toString()); session.close(); return result; }
@SuppressWarnings("unchecked") private void addNonResolved(List<Project> projects) { String queryString = "select m as message from MTorMessage as m where m.project.id = :projectId AND m.resolved = :resolved"; LOG.trace("retrieving messages with query: " + queryString); if (projects == null) { return; } for (Project project : projects) { LOG.trace("retrieving messages for project: " + project); Query query = getSession().createQuery(queryString); query.setLong("projectId", project.getId()); query.setBoolean("resolved", false); HashSet<MTorMessage> messges = new HashSet<MTorMessage>(query.list()); project.setMessages(messges); } }
public List<ChiTietHoaDon> layDS(String maTaiKhoan, int page, int nRecordsPerPage) { List<ChiTietHoaDon> ds; Session session = openSession(); String hql_hd = "from ChiTietHoaDon cthd left join fetch cthd.hoaDon where cthd.hoaDon.taiKhoan.maTaiKhoan=:maTaiKhoan and cthd.hoaDon.daXoa=:trangThai order by cthd.hoaDon.ngayLap desc"; // "from HoaDon hd left join fetch hd.chiTietHoaDons where hd.taiKhoan.maTaiKhoan=:maTaiKhoan and hd.daXoa=:trangThai"; Query query = session.createQuery(hql_hd); query.setString("maTaiKhoan", maTaiKhoan); query.setBoolean("trangThai", false); query.setFirstResult((page - 1) * nRecordsPerPage); query.setMaxResults(nRecordsPerPage); ds = query.list(); session.close(); return ds; }
protected void setParameter(Query query, String paramName, Object paramValue) { if (paramValue instanceof String) { query.setString(paramName, (String) paramValue); } else if (paramValue instanceof Integer) { query.setInteger(paramName, (Integer) paramValue); } else if (paramValue instanceof Long) { query.setLong(paramName, (Long) paramValue); } else if (paramValue instanceof Double) { query.setDouble(paramName, (Double) paramValue); } else if (paramValue instanceof Boolean) { query.setBoolean(paramName, (Boolean) paramValue); } else if (paramValue instanceof Date) { // TODO 难道这是bug 使用setParameter不行?? query.setTimestamp(paramName, (Date) paramValue); } else if (paramValue instanceof Collection) { query.setParameterList(paramName, (Collection<?>) paramValue); } else { query.setParameter(paramName, paramValue); } }
public void applySQLParameter(Query q) { Iterator<String> it = parameterMap.keySet().iterator(); while (it.hasNext()) { String key = it.next(); HQLObject obj = parameterMap.get(key); String s = ""; if (obj.obj instanceof String) { if (obj.wholeWords) { s = obj.obj.toString(); } else { s = "%" + obj.obj.toString() + "%"; } q.setString("s_" + obj.fieldName, s); } else if (obj.obj instanceof java.util.Date) { // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // s = sdf.format(obj.obj); // q.setDate("s_" + obj.fieldName, (java.util.Date)obj.obj); q.setTimestamp("s_" + obj.fieldName, (java.util.Date) obj.obj); // logger.debug("SDF: " + sdf.format(obj.obj)); } else if (obj.obj instanceof Boolean) { // s = Boolean.parseBoolean(obj.obj.toString()) ? "1" : "0"; q.setBoolean("s_" + obj.fieldName, (Boolean) obj.obj); // logger.debug("Bool: " + s); } else if (obj.obj instanceof Integer) { // s = ((Integer)obj.obj).toString(); q.setInteger("s_" + obj.fieldName, (Integer) obj.obj); } else if (obj.obj instanceof Long) { // s = ((Integer)obj.obj).toString(); q.setLong("s_" + obj.fieldName, (Long) obj.obj); } else { s = obj.obj.toString(); q.setString("s_" + obj.fieldName, s); logger.warn("Typ nicht gefunden: " + obj.obj.getClass().getCanonicalName()); } } }