简体   繁体   中英

Spring Hibernate JPA Bidirectional OneToOne - not updating id on delete

I have 2 entities in a OneToOne relationship:

A User entity :

@Entity
public class User{

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

    @OneToOne
    @JoinColumn()
    private StudentInfo studentInfo;
}

And a StudentInfo entity:

@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) ; , 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.

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 ?

(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.

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. This is true for all sql databases and has nothing to do with Spring or JPA.

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