如何处理org.hibernate.ObjectNotFoundException

时间:2015-02-18 07:48:49

标签: java hibernate spring-mvc

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

@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表中的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表中使用trip id = 0的任何选项,没有任何例外吗?我怎么能允许这个?

1 个答案:

答案 0 :(得分:1)

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