简体   繁体   中英

How to get latest 5 results from MySQL database using Spring,Hibernate

I need some help to fix my issue. I'm having a REST webservices project with Spring and Hibernate . By executing the POST call in POSTMAN ,I've inserted some details into MySQL database. So, now i need to get latest 4(which means last 4) from the database table using GET

---id--------|----testname---|----testmethod----|---groupname---|-result----

So, these above mentioned are the columns of my database table. so i need to get last 4 results based on column groupname . So, i need to search by groupname and id . The values of groupname will be like [Test] . So, can anyone tell is there any way to get those details through GET call.

If you want to retrieve some specific number of rows from database using hibernate then you can do something like this

Criteria cr = session.createCriteria(YourClass.class);
cr.setFirstResult(1);
cr.setMaxResults(4);
List results = cr.list();

The above code will retrieve first 4 rows from your DB.

If you want to retrieve last 4 rows then you have to somehow count the number of rows then use code like this

Criteria cr = session.createCriteria(YourClass.class);
cr.setFirstResult(count-4);
cr.setMaxResults(count);
List results = cr.list();

it will give you last 4 rows from your DB.

if you want to use HQL then you can do something like this

Query q = session.createQuery("FROM table");
q.setFirstResult(start);
q.setMaxResults(maxRows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM