How to make a sql count in the Map?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
11 views
Task: to read books outside the genre and bring them out.

Can't correctly count them. First, try to simply count the number but here is the problem.

public List task4() { Session Session = this.sessionFactory.getCurrentSession(); String sql = "SELECT book.genre, COUNT(book.genre) AS counter FROM book book GROUP BY genre"; SQLQuery query = session.createSQLQuery(sql); query.addEntity(Book.class); // List task4List = session.createQuery("SELECT book.genre, COUNT(book.genre) AS counter FROM book GROUP BY genre") // .list(); List task4List = query.list(); //Map task4List = (Map). return task4List; }

Thought to use the Mape because it is perfect under it.
How to count books by genre on-demand?
Suitable criteria, hql or native query.
by | 11 views

2 Answers

0 like 0 dislike
The key question - for what purpose are interested?

There are many options, just for instance:
  1. Make a separate table with genres, and triggers to maintain counters (according to analogy, make the functions of the tags). Triggers may be outside of the DBMS to write a couple of functions that will be added, incremented and decremented
  2. Select all and count hands. The result can be cached
  3. To choose one genre and return the result size
by
0 like 0 dislike
Something like this? This is the jpql
@Query("SELECT COUNT(b) FROM Book b WHERE b.genre = ?1") Long booksCount(String genre);
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked May 10, 2019 by SlandShow
0 like 0 dislike
3 answers
asked Apr 14, 2019 by TonyJa
110,608 questions
257,186 answers
0 comments
35,390 users