When I save a new entity, it's Primary Key ID started saving from 1, as supposed to. But there were rows on the table before I implemented the JPA.
Entity.class:
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(schema = "db", name = "table")
public class Entity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
}
Example: Let's say there are 10 rows. I .save()
a new entity and it saves with the id = 5
, and of course I get an exception.
Do I need to keep saving until the generated ID gives a + 10 number. Is this normal? Can't the @GeneratedValue
get the last id from the table and +1 itself?
The issue was with the autogenerated ID. As I did have rows on the table before implementing the JPA, the auto increment started with 1.
Fixed by modifying the auto increment sequence:
SELECT SETVAL('table_id_seq', (SELECT MAX(id) + 1 FROM table));
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.