java - How to count records of particular id in spring boot and hibernate -


i working on project using spring boot , hibernate. want count records against id in table. can count records of table unable count records against foreign key.

here controller code

@requestmapping("/rating/{id}") @responsebody   public long getratinginfo(@pathvariable("id") long id, httpservletrequest req, httpservletresponse res) throws exception {  long postobj = mydao.count(); return postobj; } 

here passing id in url there no method of count pass id find records.

and here dao interface

@transactional public interface mydaointerface extends crudrepository<rating, long>{ } 

add dao:

@query(countbypostid) integer countbypostid(long post_id);  final string countbypostid= "select count(ra) rating ra ra.post_id = ?1" 

and call likewise:

@requestmapping("/rating/{id}") @responsebody  public long getratinginfo(@pathvariable("id") long id, httpservletrequest req, httpservletresponse res) throws exception {  long postobj = mydao.countbypostid(id); return postobj; } 

edit: second question in comments:

@transactional public interface mydaointerface extends crudrepository<rating, long>{    list<rating> findbypostid(long id); } 

and caller:

@requestmapping("/rating/{id}") @responsebody  public long getratinginfo(@pathvariable("id") long id, httpservletrequest req, httpservletresponse res) throws exception {   list<rating> ratings = mydao.findbypostid(id);  long postobj = ratings.size() //use code need # of entires.   return ratings; } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -