简体   繁体   English

Spring Hibernate JPA双向OneToOne-删除时不更新id

[英]Spring Hibernate JPA Bidirectional OneToOne - not updating id on delete

I have 2 entities in a OneToOne relationship:我在 OneToOne 关系中有 2 个实体:

A User entity :一个用户实体:

@Entity
public class User{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne
    @JoinColumn()
    private StudentInfo studentInfo;
}

And a StudentInfo entity:还有一个 StudentInfo 实体:

@Entity
public class StudentInfo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne(cascade = CascadeType.REMOVE)
    @JoinColumn()
    private User user;
}

When I delete the first StudentInfo instance from the database (with id=1) using studentRepository.deleteById((long) 1) ;当我使用studentRepository.deleteById((long) 1)从数据库中删除第一个 StudentInfo 实例(id= studentRepository.deleteById((long) 1) , the instance does get deleted, so does the User associated with it, but the problem is that the ids of the other objects in the database don't get updated. ,实例确实会被删除,与之关联的用户也会被删除,但问题是数据库中其他对象的 ID 不会更新。

After deleted a student, the users in the database look like this :删除学生后,数据库中的用户如下所示:

删除后的用户

And the students remaining (only one):剩下的学生(只有一个):

删除后的学生

How can I make so that the ids are automatically updated on delete ?我怎样才能使 ID 在删除时自动更新?

(2, 3, 4) -> (1, 2, 3). (2, 3, 4) -> (1, 2, 3)。

Apparently this is quite a dumb question because while the ids in a database do have to be unique for each line in a table, they don't have to be consecutive.显然,这是一个非常愚蠢的问题,因为虽然数据库中的 id 对于表中的每一行都必须是唯一的,但它们不必是连续的。

As such, when a registry (a line) in a database table is deleted, the other registries's ids don't get updated because there is no need for it.因此,当数据库表中的注册表(一行)被删除时,其他注册表的 id 不会更新,因为不需要它。 This is true for all sql databases and has nothing to do with Spring or JPA.这对所有sql数据库都是如此,与Spring或JPA无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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