Hibernate随机获取指定范围内的指定条目的记录

下面这段代码是我最近写项目用到的,功能是查找用户列表

限制条件是:指定用户周边、条数限制、随机获取、指定范围


public List<User> listUserByLocation(String username, double longitude,
        double latitude, int distance, int size) {
    Location location = new Location(longitude, latitude, distance);
    String hql="from User u where u.username <> '"+username+"' and u.longitude between "+location.getMinLongitude()+" and "+location.getMaxLongitude()+" and u.latitude between "+location.getMinLatitude()+" and "+location.getMaxLatitude()+" order by rand()";
    List<User> users=hibernateTemplate.getSessionFactory().openSession().createQuery(hql).setMaxResults(size).list();
    return users;
}


本文出自 “老谢” 博客,谢绝转载!

你可能感兴趣的:(java,条目,hibernat)