I have below 3 models:
Model 1: Booking
@Entity public class Reservation { public static final long NOT_FOUND = -1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long id; @OneToMany(mappedBy = "reservation", cascade = CascadeType.ALL, orphanRemoval = true) public List<RoomReservation> roomReservations = new ArrayList<>(); }
Model 2: Room Reservation:
public class RoomReservation extends{ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long id; @JsonIgnore @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "RESERVATION_ID") public Reservation reservation; @OneToMany(mappedBy = "roomReservation", cascade = CascadeType.ALL, orphanRemoval = true) public List<GuestDetails> guestDetails = new ArrayList<>(); }
Model 3: Details for guests:
public class GuestDetails { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public Long id; public Long guestId; @JsonIgnore @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ROOM_RESERVATION_ID") public RoomReservation roomReservation; public Boolean isPrimary; @Transient public Guest guest; }
The relationships between the three are as follows:
Reservation - from one to many at RESERVATION_ID → Booking a room - from one to many at ROOM_RESERVATION_ID → Information for guests
I get the reservation object and try to update the guest information, I get the following error:
org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.model.GuestDetails.roomReservation -> com.model.RoomReservation at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1760) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:82) at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) ... 73 common frames omitted
I changed cascadeType to EVERYTHING as suggested in the general question, still getting the same error. Please do not duplicate, because I tried all the solutions that were resolved on this already asked question
Please let me know what mistake I am making. Thanks
Code to save the reservation object by modifying GuestDetails:
Reservation existingReservation = reservationRepository.findOne(reservationId); Reservation reservation = reservationParser.createFromJson(reservationNode); existingReservation.roomReservations.forEach(roomReservation -> { RoomReservation updatedRoomReservation = reservation.roomReservations.stream().filter(newRoomReservation -> Objects.equals(roomReservation.id, newRoomReservation.savedReservationId)).findFirst().orElse(null); if(updatedRoomReservation != null){ roomReservation.guestDetails = updatedRoomReservation.guestDetails; } }); reservationRepository.save(existingReservation);
java spring spring-boot spring-data-jpa hibernate
utsav anand
source share