Java 我们如何使用旧版本的 Hibernate (~2009) 计算行数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1372317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:16:47  来源:igfitidea点击:

How do we count rows using older versions of Hibernate (~2009)?

javahibernatecount

提问by craftsman

For example, if we have a table Books, how would we count total number of book records with hibernate?

例如,如果我们有一个 Books 表,我们如何使用 hibernate 计算书籍记录的总数?

采纳答案by Salandur

For older versions of Hibernate (<5.2):

对于旧版本的 Hibernate (<5.2):

Assuming the class name is Book:

假设类名是 Book:

return (Number) session.createCriteria("Book")
                  .setProjection(Projections.rowCount())
                  .uniqueResult();

It is at least a Number, most likely a Long.

它至少是一个Number,最有可能是一个Long

回答by Jon Spokes

You could try count(*)

你可以试试 count(*)

Integer count = (Integer) session.createQuery("select count(*) from Books").uniqueResult();

Where Booksis the name off the class- not the table in the database.

Books名称在哪里class- 不是数据库中的表。

回答by marioosh

In Java i usually need to return int and use this form:

在 Java 中,我通常需要返回 int 并使用这种形式:

int count = ((Long)getSession().createQuery("select count(*) from Book").uniqueResult()).intValue();

回答by Antonio

Here is what official hibernate docs tellus about this:

以下是官方休眠文档告诉我们的内容:

You can count the number of query results without returning them:

您可以计算查询结果的数量而不返回它们:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()

However, it doesn't always return Integerinstance, so it is better to use java.lang.Numberfor safety.

但是,它并不总是返回Integer实例,因此java.lang.Number为了安全起见最好使用。

回答by xrcwrn

Long count = (Long) session.createQuery("select count(*) from  Book").uniqueResult();

回答by rajadilipkolli

If you are using Hibernate 5+, then query will be modified as

如果您使用的是 Hibernate 5+,那么查询将被修改为

Long count = session.createQuery("select count(1) from  Book")
                    .getSingleResult();

Or if you Need TypedQuery

或者如果你需要 TypedQuery

Long count = session.createQuery("select count(1) from  Book",Long.class)
                        .getSingleResult();

回答by LucianoDemuru

This works in Hibernate 4(Tested).

这适用于 Hibernate 4(已测试)。

String hql="select count(*) from  Book";
Query query= getCurrentSession().createQuery(hql);
Long count=(Long) query.uniqueResult();
return count;

Where getCurrentSession() is:

getCurrentSession() 在哪里:

@Autowired
private SessionFactory sessionFactory;


private Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}

回答by Vlad Mihalcea

It's very easy, just run the following JPQL query:

这很简单,只需运行以下 JPQL 查询:

int count = (
(Number)
    entityManager
    .createQuery(
        "select count(b) " +
        "from Book b")
    .getSingleResult()
).intValue();

The reason we are casting to Numberis that some databases will return Longwhile others will return BigInteger, so for portability sake you are better off casting to a Numberand getting an intor a long, depending on how many rows you are expecting to be counted.

我们强制转换的原因Number是某些数据库会返回Long而其他数据库会返回BigInteger,因此为了可移植性,您最好转换到 aNumber并获得 anint或 a long,具体取决于您期望计算的行数。