Answer the question
In order to leave comments, you need to log in
How to set up auto-incrementing id in PostgreSql table in Spring Boot with cleaning up deleted ids?
I'm making a Java application with Spring Boot. PostgreSQL database. There is an html page that shows a table, the data is taken from a Postgresql table. After deleting/adding entries, the last row ID has a non-sequential number.
How to make the id numbers in the table go in order?
Is it possible to order the IDs using the spring annotation? Or do I need to write a special query in PostgreSql?
The class that creates the table:
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@Data
@NoArgsConstructor
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Size(min = 3, max = 30)
private String firstName;
@Size(min = 3, max = 30)
private String lastName;
@NotBlank
private String email;
@NotBlank
private String country;
@NotBlank
private String gender;
@NotBlank
private String section;
}
Answer the question
In order to leave comments, you need to log in
You don't need to do this. The whole point of identifiers is their immutability.
Thanks to everyone who answered my question!
My table in thymeleaf looks like this:
<table class="table table-bordered table-dark">
</thead>
<tbody>
<tr>
<th scope="col" >ID</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Gender</th>
<th scope="col">Email</th>
<th scope="col">Section</th>
<th scope="col">Country</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
<tr th:each="student, iStat : ${list}">
<td th:text="${iStat.index + 1}"></td>
<td th:text="${student.firstName}"></td>
<td th:text="${student.lastName}"></td>
<td th:text="${student.gender}"></td>
<td th:text="${student.email}"></td>
<td th:text="${student.section}"></td>
<td th:text="${student.country}"></td>
<td><a th:href="@{'/editstudent/' + ${student.id}}">Edit</a></td>
<td><a th:href="@{'/deletestudent/' + ${student.id}}">Delete</a></td>
</tr>
</tbody>
</table>
<tr th:each="student : ${list}">
<td th:text="${student.id}"></td>
<tr th:each="student, iStat : ${list}">
<td th:text="${iStat.index + 1}"></td>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question