通过Hibernate从SQL数据库中获取随机对象 [英] Get random object from SQL database via Hibernate

查看:418
本文介绍了通过Hibernate从SQL数据库中获取随机对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下依赖MySql的代码(ORDER BY RAND())。我想知道是否有Hibernate HQL替代它(管理员是布尔标签,表明用户作为管理员)。这是工作代码:

  public long getRandomAdmin(){
Session session = getSession();
Query selectQuery = session.createSQLQuery(SELECT user_id FROM users WHERE admin ='1 ORDER BY RAND());
selectQuery.setMaxResults(1);

列表< BigInteger> list = null;
尝试{
list = selectQuery.list();
} catch(HibernateException e){
log.error(e);
抛出SessionFactoryUtils.convertHibernateAccessException(e);


if(list.size()!= 1){
log.debug(getRandomAdmin did not find any user);
返回0;
}
log.debug(found:+ list.get(0));

return list.get(0).longValue();


解决方案

查看此链接:
http:/ /www.shredzone.de/cilla/page/53/how-to-fetch-a-random-entry-with-hibernate.html

 标准限制= yourRestrictions; 
Object result = null; //稍后将包含随机实体
Criteria crit = session.createCriteria(Picture.class);
crit.add(限制);
crit.setProjection(Projections.rowCount());
int count =((Number)crit.uniqueResult())。intValue();
if(0!= count){
int index = new Random()。nextInt(count);
crit = session.createCriteria(Picture.class);
crit.add(限制);
result = crit.setFirstResult(index).setMaxResults(1).uniqueResult();
}

这就是你想要的。保持Hibernate作为一个抽象层,同时仍然能够查询一个随机对象。但是,性能会受到一些影响。

尽管我一直在使用Hibernate,但我不知道更简单易用的优雅方式。 Imho你应该将该方法包装在外观后面。


I have following MySql dependent code ( ORDER BY RAND() ) . I would like to know if there is hibernate HQL alternative for it (admin is boolean tag indicating that the user as an admin). This is working code:

public long getRandomAdmin() {
    Session session = getSession();
    Query selectQuery = session.createSQLQuery("SELECT user_id FROM users WHERE admin = '1' ORDER BY RAND()");
    selectQuery.setMaxResults(1);

    List<BigInteger> list = null;
    try {
        list = selectQuery.list();
    } catch (HibernateException e) {
        log.error(e);
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }

    if (list.size() != 1) {
        log.debug("getRandomAdmin didn't find any user");
        return 0;
    }
    log.debug("found: " + list.get(0));

    return list.get(0).longValue();
}

解决方案

See this link: http://www.shredzone.de/cilla/page/53/how-to-fetch-a-random-entry-with-hibernate.html

Criterion restriction = yourRestrictions;
Object result = null;  // will later contain a random entity
Criteria crit = session.createCriteria(Picture.class);
crit.add(restriction);
crit.setProjection(Projections.rowCount());
int count = ((Number) crit.uniqueResult()).intValue();
if (0 != count) {
  int index = new Random().nextInt(count);
  crit = session.createCriteria(Picture.class);
  crit.add(restriction);
  result = crit.setFirstResult(index).setMaxResults(1).uniqueResult();
}

This is what you want. Keep Hibernate as an abstraction layer while still being able to query a random object. Performance suffers a bit, though.

Although I've been using Hibernate a lot, I don't know a more elegant way that is easy to use. Imho you should wrap that method behind a facade.

这篇关于通过Hibernate从SQL数据库中获取随机对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆