object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:Spring Data JPA

时间:2017-10-20 11:29:40

标签: java spring hibernate spring-boot spring-data-jpa

我有以下3个型号:

模型1:预订

    @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<>();
}

模特2:房间预订:

 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<>();
    }

模型3:访客详情:

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;

}

这三者之间的关系如下:

预订 - RESERVATION_ID上的多个人 - &gt;客房预订 - ROOM_RESERVATION_ID上的一对多 - &gt;客人详细信息

我收到预订对象并尝试更新客人详细信息,我收到以下错误:

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

我已经将cascadeType更改为ALL,因为常见问题中的建议仍然会出现相同的错误。请不要让它重复,因为我已经尝试了所有解决这个问题的解决方案已经问过

请让我知道我在做什么错。感谢

通过更改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);

5 个答案:

答案 0 :(得分:1)

... save the transient instance before flushing : 
    com.model.GuestDetails.roomReservation -> com.model.RoomReservation

此异常明确指出RoomReservation中包含的GuestDetails在数据库中不存在(很可能idnull)。

通常,您可以通过以下方式解决此异常:

  • 在保存GuestDetails之前保存RoomReservation实体

  • cascade = CascadeType.ALL {CascadeType.MERGE, CascadeType.PERSIST}

  • 制作@ManyToOne(或至少GuestDetail-->RoomReservation

但首先,我有几点要说明:

  • 请勿在班级中使用公开字段,这违反了the encapsulation concept

  • 虽然您有双向关联,但您可以在Setter方法中设置关联的另一面。

对于您的情况,您应该更改RoomReservation类:

public class RoomReservation{

    //..... other lines of code

    @OneToMany(mappedBy = "roomReservation", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<GuestDetails> guestDetails = new ArrayList<>();

    public void setGuestDetails(List<GuestDetails> guestDetails) {

           this.guestDetails.clear();

           // Assuming that by passing null or empty arrays, means that you want to delete
           // all GuestDetails from this RoomReservation entity
           if (guestDetails == null || guestDetails.isEmpty()){
               return;
           }

           guestDetails.forEach(g -> g.setRoomReservation(this));
           this.guestDetails.addAll(guestDetails);
    }

    public List<GuestDetails> getGuestDetails() {
        // Expose immutable collection to outside world  
        return Collections.unmodifiableList(guestDetails);
    }

    // You may add more methods to add/remove from [guestDetails] collection
}

保存预订:

Reservation existingReservation = reservationRepository.findOne(reservationId);
Reservation reservation = reservationParser.createFromJson(reservationNode);
existingReservation.roomReservations.forEach(roomReservation -> {
                    Optional<RoomReservation> updatedRoomReservation = reservation.roomReservations.stream().filter(newRoomReservation -> Objects.equals(roomReservation.id, newRoomReservation.savedReservationId)).findFirst();
                    if(updatedRoomReservation.isPresent()){
                        // roomReservation already exists in the database, so we don't need to save it or use `Cascade` property
                        roomReservation.setGuestDetails( updatedRoomReservation.get().getGuestDetails());
                    }
                });
reservationRepository.save(existingReservation);

希望它有所帮助!

答案 1 :(得分:0)

GuestDetails - 添加所需的CasadeType:

@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name = "ROOM_RESERVATION_ID")
public RoomReservation roomReservation;

RoomReservation - 添加nedded CascadeType:

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.AL)
@JoinColumn(name = "RESERVATION_ID")
public Reservation reservation;

然后你需要在使用for-each循环之前/之后保留数据。取决于你safe() - 方法。

Reservation reservation = reservationParser.createFromJson(reservationNode);
entityManager.persist(reservation);

然后保护它。告诉我你的结果。也许直接工作而不改变/添加级联类型。

答案 2 :(得分:0)

您可以保存从Json获得的预订。 JPA将更新具有相同ID的行。

您得到的错误是因为guestDetails仍然引用了updatedRoomReservation。 如果您不想从json保存整个预订,则必须设置正确的RoomReservation。

例如:

if(updatedRoomReservation != null){
    roomReservation.guestDetails = updatedRoomReservation.guestDetails;
    guestDetails.forEach(guestDetail -> guestDetail.roomReservation = roomReservation);
}

答案 3 :(得分:0)

如果您使用的是JPA 2.0,那么OneToMany的默认提取类型是LAZY。如果在你的lambda之后,你的updatedRoomReservation null (当你在orElse中设置时),那么existingReservation.roomReservation.guestDetails将永远不会被加载并且将为null。

因此,当您保存existingReservation时,您会收到错误。

答案 4 :(得分:0)

这可能是由于错误的交易语义引起的。

如果在当前事务中未获取引用的实例,则将其视为瞬态。

最简单的解决方案是将@Transactional添加到方法中:

@Transactional
public void saveReservation(...) {
    Reservation existingReservation = reservationRepository.findOne(reservationId);
    Reservation reservation = reservationParser.createFromJson(reservationNode);
    // ...
    reservationRepository.save(existingReservation);
}