I am working on pagination using spring boot. I am trying get total count of results from entity manager query for the pagination. For that I am getting maxresults to show from query, but how can i get total count of results.Please tell me i am new to enity manager.
#Override
public List<User> getsearchresults(String result) {
String query = "FROM User WHERE (education = :education) OR (city = :city)";
return em
.createQuery(query,User.class)
.setParameter("education",education)
.setParameter("city",city)
.setMaxResults(5)
.getResultList();
}
this is how i write query which returns results but i need total number of result count.How can i do that plesase help me.
Related
In my DAO repository class below is the implementation
//Return the number of rows affected
public int deleteFeed(long feedId) {
String sqlQuery = "UPDATE feed SET trash = TRUE WHERE id = :id";
MapSqlParameterSource paramSource = new MapSqlParameterSource();
paramSource.addValue("id", feedId);
return namedParameterJdbcTemplate.update(sqlQuery, paramSource);
}
For the same feedId passed every time, the count result comes as 1 for the number of rows affected. If I run the same query in MySQL workbench, I am getting 0 rows affected.
Note: I am pointing to same DB in app and workbench.
I have created in a Spring Boot project a dynamic function that builds dynamic JPA queries with pagination. The main and count query are built correctly at runtime, I have even tested them directly in DB and they work fine. The problem is when the count statement is executing, the query never respond and does not show error messages. The count query built at runtime takes 4565ms in SQLDeveloper tool.
I share a code snippet and an image of where the logs are stopped...
Page<T> resultPage;
....
//create main JPA query
Query mainQuery = coreEntityManager.createQuery(queryBuilder.toString(), resultClass);
//create count JPA query
Query countQuery = coreEntityManager.createQuery(countQueryBuilder);
//set pageable params
int pageNumber = pageable.getPageNumber();
int pageSize = pageable.getPageSize();
//set first result cursor in main query
mainQuery.setFirstResult((pageNumber) * pageSize);
//set max result values in main query
mainQuery.setMaxResults(pageSize);
//set parameters in queries
for (String key : parameterMap.keySet()) {
//main query
mainQuery.setParameter(key, parameterMap.get(key));
//count query
countQuery.setParameter(key, parameterMap.get(key));
}
//run main query
List<T> fooList = mainQuery.getResultList();
//check result
if (fooList == null || fooList.size() == 0) {
//if empty return
resultPage = new CustomPage<T>(new ArrayList<T>());
} else {
//if contains values return pageable values
Long size= (Long) countQuery.getSingleResult();
log.debug("Size result query: {}",size);
resultPage = new CustomPage<T>(fooList, pageable, size);
}
return resultPage;
I am using Spring Boot + MongoDB. I need to query database based on some criteria where my methods looks like below:
#Override
public List<MyCollection> findBuyByCriteria(Request request) {
Query search = new Query();
search.addCriteria(Criteria.where("ItmId").in(request.getItmIds()));
return mongoTemplate.find(search, MyCollection.class);
}
Problem that I am facing is:
At line
search.addCriteria(Criteria.where("ItmId").in(request.getItmIds()));
request.getItmIds has 1 million Ids due to which I am getting an exception
org.bson.BsonMaximumSizeExceededException: Document size of 46282052 is larger than maximum of 16793600
Can anyone help me with this one?
If you are using Spring Data JPA, you can do something like:
findBySomeField(String someField)
If you have a more complex query, you can actually use JPQL and write a custom query.
#Query(value = "SELECT o.* from SomeObject o WHERE :someField IS NULL OR o.someField = :somefield)
public findBySomeField(#Param("someField") String someField);
I am having a record with some values in db(MySQL) + eclipselink. I am using following code to retrieve that record
EntityManager em = DBUtil.getEntityManager();
TypedQuery<User> query = em.createQuery("select u from User u where u.email='tomj#abc.com'", User.class);
List<User> results = query.getResultList();
for(User u:results){
System.out.println(u.getEmail());
}
the record in my sample db has a row with email 'tomj#abc.com'. Still when I print the list I get blank / empty list printed. I think I am going wrong somewhere. Can anyone tell me what is the mistake?
PS: I also tried to use getSingleResult which throws an exception.
I am working on a Spring-MVC project where I am required to search for some information with given parameters. Here is my current HQL query, which most likely wont get me data I require. :
org.hibernate.Query query = session.createQuery("From GroupNotes as " +
"n where n.ownednotes.msectionid=:msectionid and n.noteDisabled=false and n.noteInActive=false order by n.mnoteorder");
query.setParameter("msectionid", msectionid);
Query hiddenNotesQuery = session.createQuery("from GroupNotes as gn where gn.ownednotes.msectionid=:msectionid and gn.privateNoteUser=:username");
hiddenNotesQuery.setParameter("msectionid", msectionid);
hiddenNotesQuery.setParameter("username",person.getUsername());
List<GroupNotes> groupNotesList = new ArrayList<>();
groupNotesList.addAll(query.list());
groupNotesList.addAll(hiddenNotesQuery.list());
I would like something like this :
from GroupNotes as n where (n.ownednotes.msectionid=:msectionid and n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:"") or (n.ownednotes.msectionid=:msectionid and n.privateNoteUser=:username n.noteDisabled=false and n.noteInActive=false)
I hope the query I am looking for makes sense, so basically it should retrieve GroupNotes which are not disabled and not active, but also it should it should pay attention that it is only retrieving Private GroupNotes for the specific user and thus in the first bracker there is privateNoteUser as "". I don't know what I should have there.
If any doubts, kindly let me know. How can I form this query.
Update :
#Override
public List<GroupNotes> listGroupNotesBySectionId(int msectionid) {
Person person = this.personService.getCurrentlyAuthenticatedUser();
Session session = this.sessionFactory.getCurrentSession();
org.hibernate.Query query = session.createQuery("From GroupNotes as " +
"n where n.ownednotes.msectionid=:msectionid and ((n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:blank) or (n.privateNoteUser=:username and n.noteDisabled=false and n.noteInActive=false)) order by n.mnoteorder");
query.setParameter("msectionid", msectionid);
query.setParameter("msectionid", msectionid);
query.setParameter("username",person.getUsername());
query.setParameter("blank",null);
List<GroupNotes> groupNoteses = query.list();
}
I created the query like this, but I am getting back no data.
from GroupNotes as n where n.ownednotes.msectionid=:msectionid
and (
( n.noteDisabled=false and n.noteInActive=false and n.privateNoteUser=:"")
or
( n.privateNoteUser=:username n.noteDisabled=false and n.noteInActive=false)
)