为什么 "OneToOne" 关系允许在数据库中存储多个数据

Why "OneToOne" relationship allow to store more than one data in database

我正在研究 OneToOne 与 Spring MVC 的关系。在我的代码中 Person 是父 table,Address 是子 table。在 Address table 之后,我将数据保存在 Person table 中。我在地址页面中放置 list of person 下拉列表,人员列表下拉列表在子 table(Address) 中保留数据时参考。我在将数据持久化到两个 table 时没有问题,但问题是子 table 在 Address table 中插入多个具有相同外键的数据但是,我声明 OneToOne关系映射,所以,为什么Hibernate在Address table.

中存储多个数据时不会产生错误

下面是我的代码:

实体

public class Person {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long p_id;

    private String name;
    private String surname;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "person")
    private Address address;

    // getter setter
}
public class Address {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long a_id;

    private String district;
    private String city;

    @OneToOne(cascade = CascadeType.ALL, targetEntity = Person.class)
    @JoinColumn(name = "p_id")
    private Person person;

    // getter setter
}

控制器


// add person in database
@RequestMapping(value = "/addperson", method = RequestMethod.POST)
public String addPerson(Model mdl, @ModelAttribute("persons") Person person)
{
    pojoService.addPerson(person);
    return "redirect:/persons";
}

// add address in database
@RequestMapping(value = "/addaddress", method = RequestMethod.POST)
public String addAddress(Model mdl, @ModelAttribute("address") Address address)
{
    pojoService.addAddress(address);
    return "redirect:/address";
}

addperson(百里香)

<form th:action="@{/addperson}" th:object="${person}" method="post">
        <div class="container">
          <h1 style="text-align: center">Add Person</h1>
          <div class="row">
            <div class="col-sm-12">
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">Person name</label>
                  <input type="text" class="form-control" name="name" th:field="*{name}">
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">Person surname</label>
                  <input type="text" class="form-control" name="surname" th:field="*{surname}">
                </div>
                
                <input class="btn btn-primary" type="submit" value="Submit">
                
                <br>
                <a th:href="@{/}">Home</a>
            </div>
          </div>
        </div>
    </form>

addaddress(百里香)

<form th:action="@{/addaddress}" th:object="${address}" method="post">
        <div class="container">
          <h1 style="text-align: center">Add Address</h1>
          <div class="row">
            <div class="col-sm-12">
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">Student Name</label>
                  <select th:field="*{person}" class="form-select" aria-label=".Default select example">
                    <th:block th:each="personList: ${person}">
                        <option th:text="${personList.name + ' ' + personList.surname}" th:value="${personList.p_id}"></option>
                    </th:block>
                  </select>
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">District</label>
                  <input type="text" class="form-control" th:field="*{district}">
                </div>
                <div class="mb-3">
                  <label for="exampleFormControlInput1" class="form-label">City</label>
                  <input type="text" class="form-control" th:field="*{city}">
                </div>
                
                <input class="btn btn-primary" type="submit" value="Submit">
                
                <br>
                <a th:href="@{/}">Home</a>
            </div>
          </div>
        </div>
    </form>

结果

人table:

地址Table:

下面我将向您展示我的 add personadd address 页面的外观:

在你的Personclass里面oneToOne映射放orphanRemoval="true" 希望这会解决您的问题

在Spring Framwork中,我们总是需要引用来存储子table中的外键。在您的情况下,在子 table 中存储数据时不能引用任何内容。你必须引用父 table 主键,然后在子 table 中插入数据。它可以检查记录中是否已经存在数据,然后更新记录中的数据,否则在记录中插入新数据。

insert child前如何引用table?

// add person in database
@RequestMapping(value = "/addperson", method = RequestMethod.POST)
public String addPerson(Model mdl, @ModelAttribute("persons") Person person)
{
    pojoService.addPerson(person);
    return "redirect:/persons";
}

// add address in database
@RequestMapping(value = "/addaddress", method = RequestMethod.POST)
public String addAddress(Model mdl, @ModelAttribute("address") Address address)
{
    Person person = address.getPerson();
    person.setAddress(address); 
    pojoService.addAddress(address);
    return "redirect:/address";
}