I don’t understand why the entered data from the html template is not saved in the database.
I have an entity Supplies:
@Getter
@Setter
@Builder
@AllArgsConstructor
@Entity(name = "supplies")
public class Supplies {
public Supplies() {
this.badges = new ArrayList<>();
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "sup", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Badge> badges;
@OneToOne
@JoinColumn(name = "header_id", nullable = false)
private InputHeader header;
}
and entity Badge:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Entity(name = "badges")
public class Badge {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String badgeMaterial; //материал изготовления - тут всего два варианта ПЕЧАТНЫЙ ПЛАСТИКОВЫЙ
private String size; //размер - варианты для списка или вписываем вручную(можно оставлять пустым)
private String chroma; //цветность - варианты для списка или вписываем вручную(можно оставлять пустым)
private String density; //плотность - варианты для списка или вписываем вручную(можно оставлять пустым)
private String lamination; //показател ламинации - варианты для списка или вписываем вручную(можно оставлять пустым)
private String laminationKind; //тип ламинации - тут всего два варианта МАТОВЫЙ ГЛЯНЦЕВЫЙ
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "supplies_id", nullable = false)
private Supplies sup;
}
Look at my HTML template:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Input Supplies</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
</head>
<body>
<div class="container">
<h1>Input Supplies</h1>
<form th:action="@{/input/saveSupplies}" th:object="${supplies}" method="post">
<input type="hidden" name="headerId" th:value="${headerId}" />
<input type="hidden" name="suppliesId" th:value="${suppliesId}" />
<div class="badge-fields">
<h2>Badge 1</h2>
<div class="form-group">
<label>Badge Material</label>
<select th:field="*{badges[0].badgeMaterial}" class="form-control">
<option value="" selected> -- Select Material -- </option>
<option value="PRINTED">Printed</option>
<option value="PLASTIC">Plastic</option>
</select>
</div>
<div class="form-group">
<label>Size</label>
<input type="text" th:field="*{badges[0].size}" class="form-control" />
</div>
<div class="form-group">
<label>Chroma</label>
<input type="text" th:field="*{badges[0].chroma}" class="form-control" />
</div>
<div class="form-group">
<label>Density</label>
<input type="text" th:field="*{badges[0].density}" class="form-control" />
</div>
<div class="form-group">
<label>Lamination</label>
<input type="text" th:field="*{badges[0].lamination}" class="form-control" />
</div>
<div class="form-group">
<label>Lamination Kind</label>
<select th:field="*{badges[0].laminationKind}" class="form-control">
<option value="" selected> -- Select Lamination Kind -- </option>
<option value="MATTE">Matte</option>
<option value="GLOSSY">Glossy</option>
</select>
</div>
</div>
<div id="badge2Fields" class="badge-fields">
<h2>Badge 2</h2>
<div class="form-group">
<label>Badge Material</label>
<select th:field="*{badges[1].badgeMaterial}" class="form-control">
<option value="" selected> -- Select Material -- </option>
<option value="PRINTED">Printed</option>
<option value="PLASTIC">Plastic</option>
</select>
</div>
<div class="form-group">
<label>Size</label>
<input type="text" th:field="*{badges[1].size}" class="form-control" />
</div>
<div class="form-group">
<label>Chroma</label>
<input type="text" th:field="*{badges[1].chroma}" class="form-control" />
</div>
<div class="form-group">
<label>Density</label>
<input type="text" th:field="*{badges[1].density}" class="form-control" />
</div>
<div class="form-group">
<label>Lamination</label>
<input type="text" th:field="*{badges[1].lamination}" class="form-control" />
</div>
<div class="form-group">
<label>Lamination Kind</label>
<select th:field="*{badges[1].laminationKind}" class="form-control">
<option value="" selected> -- Select Lamination Kind -- </option>
<option value="MATTE">Matte</option>
<option value="GLOSSY">Glossy</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</body>
</html>
in my GetMapping endpoint i create all entities
@GetMapping("/input/supplies")
public String showInputSupplies(@RequestParam("headerId") Long headerId, Model model) {
Supplies supplies = new Supplies();
supplies.setHeader(inputService.getInputHeaderById(headerId));
inputService.saveInputSupplies(supplies);
Long suppliesId = supplies.getId();
System.out.println(supplies);
System.out.println(headerId);
System.out.println(suppliesId);
Badge badge1 = new Badge();
Badge badge2 = new Badge();
badge1.setSup(inputService.getSuppliesById(suppliesId));
badge2.setSup(inputService.getSuppliesById(suppliesId));
supplies.getBadges().add(badge1);
supplies.getBadges().add(badge2);
inputService.saveBadge(badge1);
inputService.saveBadge(badge2);
model.addAttribute("suppliesId", suppliesId);
model.addAttribute("supplies", supplies);
// model.addAttribute("badge1", new Badge());
// model.addAttribute("badge2", new Badge());
model.addAttribute("badge1", badge1);
model.addAttribute("badge2", badge2);
model.addAttribute("headerId", headerId);
return "inputSupplies";
}
I check in the database and see that I have a Supplies instance created with the generated ID, the HeaderID is correctly linked, two Badge instances are also created, also with the generated IDs and the correct binding to Supplies. Next, I go to my template, fill it with data and save it, after which I am redirected to return from PostMapping.
@PostMapping("/input/saveSupplies")
public String saveSupplies(@RequestParam("headerId") Long headerId,
@ModelAttribute Supplies supplies,
@ModelAttribute("badge1") Badge badge1,
@ModelAttribute("badge2") Badge badge2,
@RequestParam("suppliesId") Long suppliesId,
Model model) {
model.addAttribute("message", "Supplies and Badges saved successfully");
return "redirect:/staff?headerId=" + headerId;
}
but in the database, all filled fields remain null. What am I doing wrong?