ormone-to-one

How can set which data in this Entity will be save to the DB (Mysql)


@Entity
@Table(name = "verification_token")
public class VerificationTokenEntity {
    private static final int EXPIRATION = 60 * 24;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;

    @Column(name="token")
    private String token;

    @OneToOne(targetEntity = BillEntity.class, fetch = FetchType.EAGER)
    @JoinColumn(name = "bill_id", nullable = false, referencedColumnName = "id")
    private BillEntity billEntity;

    @Column(name="created_date")
    private Date createdDate;

    @Column(name="expiry_date")
    private Date expiryDate;
@Entity
@Data
@Table(name = "bill", schema = "bigproject")
public class BillEntity {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Basic
    @Column(name = "gender")
    private String gender;
    @Basic
    @Column(name = "name")
    private String name;
    @Basic
    @Column(name = "phone")
    private String phone;
    @Basic
    @Column(name = "email")
    private String email;
    @Basic
    @Column(name = "info")
    private String info;
    @Basic
    @Column(name = "status_id")
    private Long statusId;
    @Basic
    @Column(name = "room_id")
    private Long roomId;
}

enter image description here When i save this verificationEntity into the Database, it always save the room_id from the billEntity to the bill_id field. How can set which attribute will be save to the Database? enter image description here enter image description here

How can set which attribute will be save to the Database?


Solution

  • @RequestMapping(method = RequestMethod.GET, value = "/booking/{id}")
    public String showFormBooking(@PathVariable Long id , Model model){
        BillDto billDto = new BillDto();
        model.addAttribute("id",id);
        model.addAttribute("billDto",billDto);
        RoomDto roomDto = roomService.getDetailById(id);
        model.addAttribute("roomDto",roomDto);
        ResortDto resortDto = resortService.getDetailById(roomDto.getResortId());
        model.addAttribute("resortDto",resortDto);
        ResortImageDto resortImageDto = resortImageService.getFirstByResortId(roomDto.getResortId());
        model.addAttribute("resortImageDto",resortImageDto);
        return "frontend/booking.html";
    }
    

    I figure out that bill_id field got the wrong value is not because it come from the room_id from BillEntity

    Turn out it come from the id of BillEntity itself. Before, the id of the BillEntity always null before it's saved into the DB, the way i code, then it will get a id automatically with the auto_increment.

     <form th:action="@{/booking/} + ${roomDto.id}"
           th:object="${billDto}" th:method="post">
    

    But this time, when the billDto is posted, it take @PathVariable Long id (this is supposed to be room_id) as billDto ('id')

    So to fix this, i simply change all the pathvariable name from id -> roomid

        @RequestMapping(method = RequestMethod.GET, value = "/booking/{roomid}")
    public String showFormBooking(@PathVariable Long roomid , Model model){
        BillDto billDto = new BillDto();
        model.addAttribute("roomid",roomid);
        model.addAttribute("billDto",billDto);
        RoomDto roomDto = roomService.getDetailById(roomid);
        model.addAttribute("roomDto",roomDto);
        ResortDto resortDto = resortService.getDetailById(roomDto.getResortId());
        model.addAttribute("resortDto",resortDto);
        ResortImageDto resortImageDto = resortImageService.getFirstByResortId(roomDto.getResortId());
        model.addAttribute("resortImageDto",resortImageDto);
        return "frontend/booking.html";
    }