我正在研究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>
添加地址(百里香葉)
<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頁面的外觀:


uj5u.com熱心網友回復:
在 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";
}
uj5u.com熱心網友回復:
在你的Person類里面oneToOne映射放orphanRemoval="true"
希望這會解決你的問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/425723.html
上一篇:SpringBoot、SpringWeb、MyBatis和MySQL入門
下一篇:Hibernate使用地圖連接表
