Spring boot hibernate 中 @OneToOne 的真正目的是什么?

What is the real purpose of @OneToOne in Spring boot hibernate?

我有两个名为 UsersDependents 的实体。我想在这两个实体之间建立 OneToOne 关系。正如 OneToOne 的真正含义所述 -

Every user in the Users entity should have one and only one dependent. And every dependent in the Dependents entity should only be related to one and only one user.

但是当我将@OneToOne 添加到 Dependents 实体时,它并没有阻止我向同一用户添加两个依赖项。 @OneToOne的真正用途是什么 或任何其他关系注释,如@ManyToMany、@OneToMany、@ManyToOne?

Users.java

@Entity
@Table
public class Users {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;
    private String teamName;
    private Integer salary;

    public Users() {
    }

    public Users(Integer id, String name, String teamName, Integer salary) {
        this.id = id;
        this.name = name;
        this.teamName = teamName;
        this.salary = salary;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTeamName() {
        return teamName;
    }

    public void setTeamName(String teamName) {
        this.teamName = teamName;
    }

    public Integer getSalary() {
        return salary;
    }

    public void setSalary(Integer salary) {
        this.salary = salary;
    }
}

Dependents.java

@Entity
@Table
public class Dependents {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String name;
    private String relationship;
    @OneToOne
    private Users user;

    public Dependents() {
    }

    public Dependents(int id, String name, String relationship) {
        this.id = id;
        this.name = name;
        this.relationship = relationship;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRelationship() {
        return relationship;
    }

    public void setRelationship(String relationship) {
        this.relationship = relationship;
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }
}

并且在我的 DependentsService.java 中,我将 Dependents 对象保存为 -

public Dependents addNewDependent(Integer userId, Dependents dependent) {
        dependent.setUser(usersRepository.getOne(userId));
        return dependentsRepository.save(dependent);
}

在这里,我使用传递的 userId 从 Users 实体中获取用户并将其存储在 Dependents 对象中。当我为两个或更多依赖者传递相同的 userId 时,它将从 Users 实体中获取相同的用户并将其存储在 Dependents 实体中。这违反了 OneToOne 关系。有人可以向我解释一下,我怎样才能实现真正的 OneToOne 关系?还请解释关系注释的真正目的是什么,比如@OneToOne、@OneToMany、@ManyToOne 和@ManyToMany?

Hibernate 不会做任何额外的检查来确保记录已经存在。您有责任编写满足 OneToOne 关系的代码(这也取决于您的 UI 屏幕)。如果您仍然想抛出一些异常,请将您的主键设为依赖 table 的外键。然后你得到数据库异常。

来自Hibernate documentation

From a relational database point of view, the underlying schema is identical to the unidirectional @ManyToOne association, as the client-side controls the relationship based on the foreign key column.

...

A much more natural mapping would be if the Phone were the parent-side, therefore pushing the foreign key into the PhoneDetails table. This mapping requires a bidirectional @OneToOne association

...

When using a bidirectional @OneToOne association, Hibernate enforces the unique constraint upon fetching the child-side. If there are more than one children associated with the same parent, Hibernate will throw a org.hibernate.exception.ConstraintViolationException.

所以你应该使用双向一对一关联。

附加信息:The best way to map a @OneToOne relationship with JPA and Hibernate