Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data - get last record from the table [duplicate]

I'm using Spring Data JPA and I would like to retrieve the last record from Settings table.

I have SettingsRepository with standard methods implemented by Spring Data. How to write a method (or a query) to retrieve last row from the given table?

interface SettingsRepository extends JpaRepository<Settings, Long> {     // ? } 
like image 739
k13i Avatar asked Jun 15 '18 08:06

k13i


People also ask

How to get the last record from a table in MySQL?

Get the last record from a table in MySQL database with Java? To get data from MySQL database, you need to use executeQuery () method from java. First create a table in the MySQL database. Here, we will create the following table in the ‘sample’ database Now you can insert some records in the table using insert command.

How to get the last record in a list in spring?

There is no specific function to get the last record. You can, however, order in reverse (DESC on ID, for example - assuming there's an ID) and get the first record. Not the answer you're looking for? Browse other questions tagged java spring spring-data-jpa spring-data or ask your own question.

How to get last record of database table in Laravel 8?

On the basis of descending order of id value, we can get the last row from table. $last_row = DB::table ('students')->orderBy ('id', 'DESC')->first (); We hope this article helped you to learn How To Get Last Record of Database Table in Laravel 8 Tutorial in a very detailed way. Online Web Tutor invites you to try Skillshare free for 1 month!

How to get the number of last records in a table?

4.if flag is set read the table with the count as index. Hope this might help you out. make the use of sy-dbcnt below the select. then u'll get no. of records and also this no. is the no. of last record. n = sy-dbcnt.


1 Answers

You should use findTopByOrderByIdDesc()

This is called named query, you can check the documentation

like image 142
Lemmy Avatar answered Sep 18 '22 14:09

Lemmy