Jak liczymy wiersze używając Hibernate?
Na przykład, jeśli mamy Table Books, jak liczymy całkowitą liczbę rekordów książki za pomocą hibernate?
8 answers
Zakładając, że nazwa klasy to Book:
return (Number) session.createCriteria("Book")
.setProjection(Projections.rowCount())
.uniqueResult();
Jest to co najmniej Number
, najprawdopodobniej Long
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-01 19:17:37
W Javie Zwykle muszę zwrócić int i użyć tej formy:
int count = ((Long)getSession().createQuery("select count(*) from Book").uniqueResult()).intValue();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-11-27 08:06:09
Oto co oficjalne dokumenty hibernate mówią nam o tym:
Możesz policzyć liczbę wyników zapytań bez zwracania ich:
( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()
Jednak nie zawsze zwraca Integer
instancję, więc lepiej jest użyć java.lang.Number
dla bezpieczeństwa.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-23 09:27:34
Możesz spróbować count(*)
Integer count = (Integer) session.createQuery("select count(*) from Books").uniqueResult();
Gdzie Books
jest nazwą z class
- nie tabelą w bazie danych.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-13 00:09:40
Long count = (Long) session.createQuery("select count(*) from Book")
.uniqueResult();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-16 05:53:10
Jeśli używasz Hibernate 5+, zapytanie zostanie zmodyfikowane jako
Long count = session.createQuery("select count(1) from Book")
.getSingleResult();
Lub jeśli potrzebujesz TypedQuery
Long count = session.createQuery("select count(1) from Book",Long.class)
.getSingleResult();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-11 07:41:33
To działa w Hibernate 4 (testowany).
String hql="select count(*) from Book";
Query query= getCurrentSession().createQuery(hql);
Long count=(Long) query.uniqueResult();
return count;
Gdzie getCurrentSession() to:
@Autowired
private SessionFactory sessionFactory;
private Session getCurrentSession(){
return sessionFactory.getCurrentSession();
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-14 10:48:26
To bardzo proste, wystarczy uruchomić następujące zapytanie JPQL:
int count = (
(Number)
entityManager
.createQuery(
"select count(b) " +
"from Book b")
.getSingleResult()
).intValue();
Powodem, dla którego rzucamy do Number
jest to, że niektóre bazy danych zwrócą Long
, podczas gdy inne zwrócą BigInteger
, więc ze względu na przenośność lepiej jest rzucić do Number
i uzyskać int
lub long
, w zależności od tego, ile wierszy spodziewasz się policzyć.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-27 06:11:23