我正在研究
OneToOne
与 Spring MVC 的关系。在我的代码中,Person
是父表,Address
是子表。我将数据保存在 Person
表中,然后保存在 Address
表中。我将 list of person
下拉列表放在地址页面中,人员列表下拉列表作为参考,同时将数据保留在子表(地址)中。我在将数据保留到两个表中时没有问题,但问题是子表在 Address
表中插入多个具有相同外键的数据,但是,我声明 OneToOne
关系映射,所以,为什么 Hibernate 在存储更多数据时不会产生错误超过 Address
表中的一项数据。
下面是我的代码:
实体
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>
添加地址(Thymeleaf)
<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>
结果
人员表:
地址表:
在这里,我将向您展示我的
add person
和 add address
页面的外观:
在你的
Person
类里面oneToOne
映射放orphanRemoval="true"
希望这能解决您的问题
在 Spring Framwork 中,我们总是需要引用来在子表中存储外键。在您的情况下,您在子表中存储数据时无法获取任何引用。然后,您必须引用父表主键,然后在子表中插入数据。它可以检查记录中是否已存在数据,然后更新记录中的数据,否则在记录中插入新数据。
插入子表之前如何获取引用?
// 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";
}