gpt4 book ai didi

java - 如何处理 org.hibernate.ObjectNotFoundException

转载 作者:行者123 更新时间:2023-12-01 11:54:00 25 4
gpt4 key购买 nike

我正在开发一个 Spring mvc 应用程序,其中使用 Trip 模型和 TripStop 模型。行程模型有行程停止模型列表。以下是我的旅行模型:

@Entity
@Table(name = "Trip")
public class TripModel {
@Id
@Column(name = "tripid")
@GeneratedValue
private int tripId;

@Column(name = "tripname")
private String tripName;

@Column(name = "tripdesc")
private String tripDesc;

@Column(name = "createdate")
private Date createDate;

@OneToMany(mappedBy = "tripModel", fetch = FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
private List<TripStopModel> tripStopList;
}

以下是我的行程停靠点模型:

@Entity
@Table(name="TripStop")
public class TripStopModel {
@Id
@Column(name="tripstopid")
@GeneratedValue
private int tripStopId;

@Column(name="datetime")
private String tripStopDateTime;

@Column(name="createts")
private Date tripStopCreateTime;

@ManyToOne(optional=true)
@JoinColumn(name="locationid")
private LocationModel locationModel;

public LocationModel getLocationModel() {
return locationModel;
}

public void setLocationModel(LocationModel locationModel) {
this.locationModel = locationModel;
}
@ManyToOne(optional=true)
@JoinColumn(name="userid")
private UserModel userModel;

@ManyToOne(optional=true)
@JoinColumn(name="tripid")
private TripModel tripModel;
}

这很好用。但是当 TripStop 表中的行程 id 为 0 时,会显示以下异常:

02:32:43,784 ERROR [stderr] (http--0.0.0.0-8080-5) org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.example.scm.model.TripModel#0]

是否有任何选项可以在 TripStop 表中使用行程 id = 0,没有任何异常(exception)?我怎么能允许这样呢?

最佳答案

tripID 默认为 0,因为您正在使用基元。切换到原始包装器,以便这些值可以默认为 null,这应该可以解决问题

关于java - 如何处理 org.hibernate.ObjectNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com