I am working on OneToOne relationship with Spring MVC. In my code Person is parent table and Address is child table. I'm persist data in Person table after that in Address table. I put list of person dropdown in address page, List of person dropdown take reference while persist data in child table(Address). I have no problem while persist data into both table but problem is child table insert more than one data with same foreign key in Address table but, I declare OneToOne Relationship Mapping, so, Why Hibernate does not produce error while store more than one data in Address table.
Here down is my code:
Entity
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
}
Controller
// 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(Thymeleaf)
<form th:action="@{/addperson}" th:object="${person}" method="post">
<div >
<h1 style="text-align: center">Add Person</h1>
<div >
<div >
<div >
<label for="exampleFormControlInput1" >Person name</label>
<input type="text" name="name" th:field="*{name}">
</div>
<div >
<label for="exampleFormControlInput1" >Person surname</label>
<input type="text" name="surname" th:field="*{surname}">
</div>
<input type="submit" value="Submit">
<br>
<a th:href="@{/}">Home</a>
</div>
</div>
</div>
</form>
addaddress(Thymeleaf)
<form th:action="@{/addaddress}" th:object="${address}" method="post">
<div >
<h1 style="text-align: center">Add Address</h1>
<div >
<div >
<div >
<label for="exampleFormControlInput1" >Student Name</label>
<select th:field="*{person}" 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 >
<label for="exampleFormControlInput1" >District</label>
<input type="text" th:field="*{district}">
</div>
<div >
<label for="exampleFormControlInput1" >City</label>
<input type="text" th:field="*{city}">
</div>
<input type="submit" value="Submit">
<br>
<a th:href="@{/}">Home</a>
</div>
</div>
</div>
</form>
Result
Person table:
Address Table:
Here down i would show you how my add person and add address page look:
CodePudding user response:
In Spring Framwork, we always have to take reference to store foreign key in child table. In your case, you can't take any reference while store data in child table. You have to take reference of parent table primary key then, insert data in child table. it can check if data is already exist in record then, data is updated in record otherwise insert new data in record.
How to take reference before 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(); // get reference from parent table
person.setAddress(address); // from parent table set child table
pojoService.addAddress(address);
return "redirect:/address";
}
CodePudding user response:
In your Person class inside oneToOne mapping put orphanRemoval="true"
Hope this will solve your issue




