Проект Spring-Boot и Thymeleaf Три сущности
@Entity
@Table(name = "peoples", schema = "public", catalog = "people")
@EqualsAndHashCode(of = "id")
@Getter
@Setter
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
public class PeopleEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private long id;
private String name;
@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "children_id")
private ChildrenEntity сhildrenEntityId;
@OneToOne(fetch=FetchType.LAZY)
private BloodgroupEntity bloodgroupEntity;
}
@Entity
@Table(name = "childrens", schema = "public", catalog = "blood_group")
...
public class BloodgroupEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private long id;
private String name;
@OneToOne
@JoinColumn(name="id")
private PeopleEntity peopleEntity;
}
@Entity
@Table(name = "peoples", schema = "public", catalog = "children")
@EqualsAndHashCode(of = "id")
@Getter
@Setter
@DynamicUpdate
@DynamicInsert
@SelectBeforeUpdate
public class ChildrenEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private long id;
private String name;
}
Таблицы:
CREATE TABLE people (
id BIGINT NOT NULL,
NAME VARCHAR(100) NOT NULL,
id_children BIGINT NOT NULL,
PRIMARY KEY ( pk_people )
)
ALTER TABLE public.people ADD CONSTRAINT fk_children FOREIGN KEY (id_children) REFERENCES children(id) ON UPDATE CASCADE
ALTER TABLE public.people ADD CONSTRAINT fk_cblood_group FOREIGN KEY (id_blood_group) REFERENCES blood_group(id) ON UPDATE CASCADE
CREATE TABLE blood_group (
id BIGINT NOT NULL,
NAME VARCHAR(100) NOT NULL,
id_children BIGINT NOT NULL,
PRIMARY KEY ( pk_blood_group )
)
CREATE TABLE children (
id BIGINT NOT NULL,
NAME VARCHAR(100) NOT NULL,
PRIMARY KEY ( pk_children )
)
Контроллеры:
private List<OperationTableEntity> peopleEntityList;
private List<ChildrenEntity> childrenEntityList;
@GetMapping(value = "/add-product-type")
public String showPeople(@ModelAttribute("peopleEntityList") PeopleEntity peopleEntity
Model model) {
peopleEntityList = peopleEntityService.getAll();
childrenEntityList = childrenEntityService.getAll();
model.addAttribute("peopleEntityList", peopleEntityList);
model.addAttribute("childrenEntityList", childrenEntityList);
return "add-people";
}
@PostMapping(value = "/people/new")
public String createPeople(@ModelAttribute("peopleEntity") PeopleEntity peopleEntity) {
peopleEntityService.save(peopleEntity);
return "redirect:/add-product-type";
}
файл add-people.html:
....
<form id="f-prt-01" th:method="POST" th:action="@{/people/new}" th:object="${peopleEntity}" name="formPrType">
<div class="form-group mt-1">
<label for="inputName">Имя</label>
<input type="text" class="form-control" id="inputName" placeholder="Имя" th:field="*{name}" autofocus required>
<label for="inputGroupSelect02">группа</label>
<div class="input-group mb-3">
<select class="custom-select" id="inputGroupSelect02" th:field="*{сhildrenEntityId}">
<option th:each="bloodgroupTable : ${bloodgroupEntityList}"
th:value="${bloodgroupTable.id}"
th:text="${bloodgroupTable.name}"/>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Сохранить</button>
</form>
Подскажите правильно я реализовал код для таблиц PeopleEntity и BloodgroupEntity. Т.е. у одного человека может быть только одна группа крови. Сделал по аналогии в обучающих курсах. Они правда у всех разные.