To perform a SELECT COUNT(*) query using Hibernate, you can use the count() method provided by the JpaRepository interface. This method returns the number of entities in the repository.
For example, suppose you have a Book entity and a BookRepository interface that extends JpaRepository<Book, Long>. You can use the count() method to get the number of books in the repository like this:
long numBooks = bookRepository.count();Source:www.lautturi.com
You can also use the @Query annotation to define a custom SELECT COUNT(*) query. For example:
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
@Query("SELECT COUNT(b) FROM Book b")
long countAllBooks();
}
You can then call the countAllBooks() method to execute the custom query and get the number of books in the repository.