[Spring,Thymeleaf] ModelAttribute nie odbiera wartości foreignkeyów

0

Cześć.
Chciałem w szybki sposób zaimplementować edycje encji rezerwacji.
Encja:

@Entity
@Table(name = "reservations")
public class ReservationEntity implements Serializable {
    @Id
    private String id;
    @ManyToOne
    @JoinColumn(name = "car_id")
    private CarEntity car;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date starts,ends;
    @NotNull
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date input;
    @ManyToOne
    @JoinColumn(name = "startLocation_id",nullable = false)
    private LocationEntity startLocation;
    @ManyToOne
    @JoinColumn(name = "endLocation_id",nullable = false)
    private LocationEntity endLocation;

HTML:

  <form th:id="'edit'+${reservation.Id}" th:action="'/admin/editReservation'" th:object="${reservation}" method="post">
<div class="container">
    <div class="row">
        <div class="col-sm-6">
            <div class="form-group">
                <label for="p1">ID:</label>
                <input type="text" th:form="'edit'+${reservation.Id}" th:value="*{Id}" name="id" class="form-control" id="p1">
            </div>
            <div class="form-group">
                <label for="p2">Klient:</label>
                <input type="text" th:form="'edit'+${reservation.Id}" th:value="*{client.id}" name="end_location_id" class="form-control" id="p2">
            </div>
            <div class="form-group">
                <label for="p3">Data wynajmu:</label>
                <input type="text"  th:form="'edit'+${reservation.Id}" th:value="*{#dates.format(starts, 'yyyy-MM-dd HH:mm')}" name="starts" class="form-control" id="p3" rows="1">
            </div>
            <div class="form-group">
                <label for="p4">Przebieg wynajmu:</label>
                <input type="text"  th:form="'edit'+${reservation.Id}" th:value="*{startMilage}" name="startmilage" class="form-control" id="p4" rows="1">
            </div>
            <div class="form-group">
                <label for="p5">Lokalizacja wynajmu:</label>
                <input type="text" th:form="'edit'+${reservation.Id}" th:value="*{startLocation.id}" name="startlocation" class="form-control" id="p5" rows="1">
            </div>
            <div class="form-group">
                <label for="p6">Opłata:</label>
                <input type="text" th:form="'edit'+${reservation.Id}" th:value="*{price}" name="price" class="form-control" id="p6" rows="1">
            </div>
            <div class="form-group">
                <label for="p7">Kaucja:</label>
                <input type="text"  th:form="'edit'+${reservation.Id}" th:value="*{caution}" name="caution" class="form-control" id="p7" rows="1">
            </div>
            <div class="form-group">
                <label for="p8">Opis:</label>
                <input type="text" th:form="'edit'+${reservation.Id}" th:value="*{description}" name="description" class="form-control" id="p8" rows="1">
            </div>
        </div> 

Kontroler:

    @PostMapping("/admin/editReservation")
    public String adminPost(@ModelAttribute ReservationEntity reservation) {
        reservationService.saveReservation(reservation);
        System.out.println(reservation.getId()+"\n"+reservation.getInput());
        return "redirect:/admin";
    }

Bład

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

Problem jest taki że w kontrolerze kilka pól w obiekcie 'reservation' jest nullami. A są to pola które w encji są foreign keyami i są zjoinowane to innych tabel.
Jak w formularzu html(thymeleaf) wybierać wartość pola które jest kluczem obcym a później poprawnie przekazać to do kontrolera?
Z powyższego kodu nullem w kontrolerze jest pole stratLocation i client.
Prośba o poradę

1

Przecież masz @JoinColumn(name = "startLocation_id",nullable = false), a dokładniej nullable = false - więc w czym zaskoczenie?

0

Tak bo założenie jest że rezerwacja musi mieć startLocation, z tym że w htmlu, w obiekcie:

th:object="${reservation}"

jest ta wartość startLocation a po wysłaniu formularza i odebrania go przez kontroler jest nullem. Tak jakby nie przechodzi z formularza do kontrolera.

/// EDIT
Rozwiązanie


                                                    <div class="form-group">
                                                        <label for="client">Klient:</label>
                                                        <input type="text" th:form="'edit'+${id}" th:value="*{{client}}" name="client" class="form-control" id="client">
                                                    </div>

Użycie podwójnych nawiasów klamrowych {{ }} rozwiązuje problem.
WAŻNE: nazwa obiektu w encji (client) musi być także jako atrybut name i id!!

1 użytkowników online, w tym zalogowanych: 0, gości: 1