How to Count the Rows in Hibernate Query Language

How to Count the rows in Hibernate Query Language?

Have you tried the query.uniqueResult(); ? As your Select count(*) will give you only one number, you should be able to retrieve it with this like int count = (Integer)query.uniqueResult();

To count based on a Criteria you can do this:

Criteria criteria = currentSession().createCriteria(type);
criteria.setProjection(Projections.rowCount());
criteria.uniqueResult();

I'm using the Criteria right now so I know for sure that it works. I saw the uniqueResult() solution on a website here: http://www.jroller.com/RickHigh/entry/hibernate_pagination_jsf_datagrid_prototype1

How to write a query in hibernate for count(*)

Suppose your login table is mapped by a LoginClass class, with emailid and password instance variables. Then you'll execute something like:

Query query = session.createQuery(
"select count(*) from LoginClass login where login.emailid=:email and login.password=:password");
query.setString("email", "something");
query.setString("password", "password");
Long count = (Long)query.uniqueResult();

It should return in count the result you're looking for. You just have to adapt the name to your class and your parameter names.

Hibernate count rows with some criterias

Criteria crit = session.createCriteria(Person.class);
crit.add( Restrictions.isNotNull("birthDate"));
crit.add( Restrictions.eq("isStudent", true));
crit.setProjection(Projections.rowCount());
Integer count = (Integer)crit.uniqueResult();

Word count in Hibernate Query Language (hql)

I don't know of a way to do this using HQL alone. As a workaround, you can read each row into Java, and then manually count the number of words in details.

If you are open to using a native query, then we can try counting words by counting the number of spaces in the details column. For example, on MySQL and Oracle, you could try the following query:

SELECT post
FROM post
WHERE LENGTH(details) - LENGTH(REPLACE(details, ' ', '')) > 98;

SQL Count in Hibernate HQL

I don't have a hibernate setup available to test, but it seems like you can:
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-aggregation

How to count row table in JPA Query

According repositories documentation using CrudRepository provides a method called count() that is one of the Superinterfaces which JpaRepository is implemented.

Based CrudRepository documentation says:

long count(); Returns the number of entities.

Then you should use CrudRepository method. In addition Remove Uppercase MembershipREPOSITORY, by java code convention, you have to use by following way MembershipRepository.

 @Repository
public interface MembershipRepository extends JpaRepository <ActIdMembershipEntity, String> {
}

And use it in your Service:

 @Service
public class MembershipService {

@Autowired
private MembershipRepository repo;

public long count() {
return repo.count();
}
}

UPDATED

Based on your requirement:
In Controller:

@RestController
public class MembershipResource {

@Autowired
private MembershipService membershipService;

@GetMapping("/membership")
public List<Object> list() { return membershipService.countMemberships();
}
}

In Service:

@Service
public class MembershipService {

@Autowired
private MemershipRepository repository;

public List<Object> countMemberships() {
return repository.countMemberships();
}

}

In Repository:

@Repository
public interface MemershipRepository extends JpaRepository<ActIdMembershipEntity, String> {
@Query ("select i.userId, count(i) from ActIdMembershipEntity i where i.userId ='kermit'")
List<Object> countMemberships();
}


Related Topics



Leave a reply



Submit