如何处理org.hibernate.ObjectNotFoundException

How to handle org.hibernate.ObjectNotFoundException

我正在开发一个 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 table 中的 trip 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 table 中毫无例外地使用 trip id = 0?我怎么能允许这样做?

tripID 默认为 0,因为您使用的是原语。切换到原始包装器,以便这些值可以默认为 null,这应该可以解决它