我有以下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<>();
}
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<>();
}
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;
}
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
请让我知道我在做什么错误。谢谢
通过更改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);
... save the transient instance before flushing :
com.model.GuestDetails.roomReservation -> com.model.RoomReservation
此异常清楚地表明,GuestDetails
中包含的RoomReservation
在数据库中不存在(很可能是ID
为NULL
)。
通常,您可以通过以下方法解决此异常:
>
在保存客房详细信息之前保存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);
希望有帮助!
我有两个例子。一个是项目,一个是模块。< code >一对多关系。 我将模块设置到项目中。然后使用。第一次,因为数据库中没有记录,所以项目和模块可以保存到数据库中。然而,第二次,因为我不需要创建新项目,我只创建新模块,然后将模块设置回项目。 发生异常。 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例。 有什么办法可以解决这个问题吗谢谢
上图显示了表之间的关系。 AddressType表包含静态值,如mailing、home、work等。 在AddressTypeRel模型类中,我有一个带有多对一注释的AddressType对象
错误: 我还尝试了,但是 病因是什么?显然,我设置了nullable=true(在数据库中也是如此),尽管我有这个错误。有什么想法吗?
user.java是 我正在使用这个视频中提到的存储库,是 和 这个问题有什么更好的解决办法吗?
我试图将用户详细信息存储到以下表中:user、role、user_role。当试图保存详细信息时,它会引发以下错误。 托管刷新时出错[org.hibernate.transientPropertyValueException:object引用未保存的瞬态实例-在刷新前保存瞬态实例 这里user_role是父表,user和role表是子表。我尝试了cascade=cascade.all。即使它也会犯
在baseEntity中具有Id
我正在使用Seam Framework。我有2个实体: Request.java 和请求事件.java 当我进行此交易时: 我得到了这个错误:
对象引用未保存的瞬态实例 - 在刷新错误之前保存瞬态实例 我有 3 个实体。我将使用一对多关系映射彼此实体。 产品收费详情有 燃料类型有 计算类型具有 我有一个问题,当我保存产品时,它抛出这样一个错误。我试着用@ManyToOne(cascade = CascadeType。ALL)到productChargeDetail,但是当我保存一个产品时,它显示一个来自应用程序的错误复制错误。FuelTy