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
Automatically Size Jpanel Inside Jframe
Java8 Way to Handle If..Else Check in Collections
How to Add Multiple Components to a Jframe
Spring Boot @Enablescheduling Conditionally
Default Value in Lombok. How to Init Default With Both Constructor and Builder
How to Change the Background Color of a Textfield Without Changing the Border in Javafx
Setting a Log File Name to Include Current Date in Log4J
Using Loop to Accept Additional User Input Until User Enters a Value That Ends the Input
A Better Way to Convert Integer (May Be Null) to Int in Java
Replacement for Wsimport With Jdk 11
Testing Two Json Objects for Equality Ignoring Child Order in Java
Convert Html/Mxml File to Word Doc Programmatically in Java
Springboot - How to Start Embedded Container
Autowiring Httpservletrequest in Spring Controller
How to Exit an Android App Programmatically
Jpa Repository Query - Java.Lang.Object; Cannot Be Cast to Model
Jackson Deserialization Issue for Zoneddatetime
How to Query Dynamodb Based on Partition Key and Sort Key [Java]