So, I have 2 entities:
Room:
@Entity
@Table(name = "rooms")
public class Room
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String type;
private String image;
private double price;
@Column(columnDefinition = "TEXT")
private String description;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "guest_id", referencedColumnName = "id")
private Guest guest_id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "guest_name", referencedColumnName = "name")
private Guest guest_name;
Guest:
@Entity
@Table(name = "guests")
public class Guest
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String phone_num;
private String email;
private LocalDate check_in_date;
private LocalDate check_out_date;
@OneToOne
@JoinColumn(name = "room_id", referencedColumnName = "id")
private Room room_id;
As you can see in Room there are 2 foreign keys which reference id and name fields in Guest and 1 foreign key in Guest which references id field in Room.
In my controller there’s a method which is supposed update an entry inside my guests table:
@PostMapping("/update/{id}")
public String updateGuest(@PathVariable Long id, @ModelAttribute("guest") Guest guest)
{
Guest existingGuest = guestRepository.findById(id).get();
existingGuest.setName(guest.getName());
existingGuest.setPhone_num(guest.getPhone_num());
existingGuest.setEmail(guest.getEmail());
existingGuest.setCheck_in_date(guest.getCheck_in_date());
existingGuest.setCheck_out_date(guest.getCheck_out_date());
guestRepository.save(guest);
return "redirect:/guests/";
}
It worked before I had foreign keys, but since there’s now a connection between fields I think I am supposed to also update fields inside rooms table. But I am honestly quite unsure how to do it.
I think I am supposed to create a method inside repository sort of like this:
@Repository
public interface RoomRepository extends JpaRepository<Room, Long>
{
@Query("Select g From rooms g Where g.guest_id = :guestId")
List<Room> findGuestById(@Param("guestId") String guestId);
}
Is my thinking here correct?
Massive thanks in advance!!