如何编写hql查询或条件查询以根据时间戳获取最新记录

时间:2014-04-07 13:54:42

标签: hibernate hql criteria

我是ORM技术的新手所以可以帮助如何编写hql或条件查询来获取基于时间戳的最新记录。 这里Eventdata域名属性名称将与下面提到的列名称相同。 我有这样的表 EVENTDATA:

AccountId   DeviceId    timestamp   Address
fuel    mdemo143    1389716109  Bangalore Road, Rajendra Nagar, Mysore, Karnataka
fuel    mdemo469    1389757662  Bharath International Travels, Diwan's Road
fuel    mdemo731    1390179714  Ring Road, Hebbalu, Mysore,
fuel    mdemo143    1390148225  Gandhinagar, Mysore


fuel    mdemo469    1389757682  Bharath International Travels, Diwan's Road
fuel    mdemo469    1389759662  Bharath International Travels, Diwan's Road
fuel    mdemo731    1390179714  Ring Road, Hebbalu, Mysore,
fuel    mdemo731    1390279714  Ring Road, Hebbalu, Mysore,

我需要输出表如下:

AccountId   DeviceId    timestamp   Address

fuel    mdemo143    1390148225  Sun, 19 Jan 2014 16:17:05 GMT   Gandhinagar, Mysore


fuel    mdemo469    1389759662  Wed, 15 Jan 2014 04:21:02 GMT   Bharath International Travels, Diwan's Road
fuel    mdemo731    1390279714  Tue, 21 Jan 2014 04:48:34 GMT   Ring Road, Hebbalu, Mysore

请帮帮我。谢谢提前

2 个答案:

答案 0 :(得分:1)

考虑使用ORDER BYQuery#setMaxResults

的内容
queryString = "SELECT e FROM Eventdata e ... ORDER BY e.timestamp";

session.createQuery(queryString).setMaxResults(10);

应该有用。

答案 1 :(得分:0)

如果您尝试根据timestamp获取记录,可以使用以下内容:

    @SuppressWarnings("unchecked")
public List<String> getData(Date d){
    Query query = em.createQuery("SELECT e FROM EventDate e WHERE e.timestamp > :d ORDER BY e.timestamp");
    query.setParameter("d", d);
    List<String> queryResults = query.getResultList();
    return queryResults;
}

这将返回指定timestamp

之后的记录