我有两个类CaretakerDetails和Devices两个实体之间存在OneToMany关系,但是当我尝试插入对象时,它会引发以下异常:
无法将值NULL插入“Caretaker_Id”列,表'Emomtracking.dbo.Device_Details';
看守班如下:
@Entity
@Table(name = "Caretaker_Details", schema = "dbo", catalog = "Emomtracking")
@DynamicUpdate
public class CaretakerDetails implements java.io.Serializable {
private Integer caretakerId;
private EmomLookup emomLookup;
private String caretakerName;
private Date createdDate;
private String caretakerNumber;
private List<DeviceDetails> deviceDetailses = new ArrayList<DeviceDetails>();
public CaretakerDetails() {
}
public CaretakerDetails(String caretakerName, Date createdDate, String caretakerNumber) {
this.caretakerName = caretakerName;
this.createdDate = createdDate;
this.caretakerNumber = caretakerNumber;
}
public CaretakerDetails(EmomLookup emomLookup, String caretakerName, Date createdDate, String caretakerNumber,
List<DeviceDetails> deviceDetailses) {
this.emomLookup = emomLookup;
this.caretakerName = caretakerName;
this.createdDate = createdDate;
this.caretakerNumber = caretakerNumber;
this.deviceDetailses = deviceDetailses;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Caretaker_Id", unique = true, nullable = false)
public Integer getCaretakerId() {
return this.caretakerId;
}
public void setCaretakerId(Integer caretakerId) {
this.caretakerId = caretakerId;
}
// @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "Pairing_Status")
public EmomLookup getEmomLookup() {
return this.emomLookup;
}
public void setEmomLookup(EmomLookup emomLookup) {
this.emomLookup = emomLookup;
}
@Column(name = "Caretaker_Name", nullable = false)
public String getCaretakerName() {
return this.caretakerName;
}
public void setCaretakerName(String caretakerName) {
this.caretakerName = caretakerName;
}
@Column(name = "Created_Date", nullable = false)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
@Column(name = "Caretaker_Number", nullable = false)
public String getCaretakerNumber() {
return this.caretakerNumber;
}
public void setCaretakerNumber(String caretakerNumber) {
this.caretakerNumber = caretakerNumber;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "caretakerDetails", cascade = CascadeType.PERSIST)
public List<DeviceDetails> getDeviceDetailses() {
return this.deviceDetailses;
}
public void setDeviceDetailses(List<DeviceDetails> deviceDetailses) {
this.deviceDetailses = deviceDetailses;
}
}
设备实体如下:
@Entity
@Table(name = "Device_Details", schema = "dbo", catalog = "Emomtracking")
public class DeviceDetails implements java.io.Serializable {
private Integer deviceId;
private CaretakerDetails caretakerDetails;
private String deviceNumber;
private Date createdDate;
public DeviceDetails() {
}
public DeviceDetails(CaretakerDetails caretakerDetails, String deviceNumber, Date createdDate) {
this.caretakerDetails = caretakerDetails;
this.deviceNumber = deviceNumber;
this.createdDate = createdDate;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "Device_Id", unique = true, nullable = false)
public Integer getDeviceId() {
return this.deviceId;
}
public void setDeviceId(Integer deviceId) {
this.deviceId = deviceId;
}
@ManyToOne
@JoinColumn(name = "Caretaker_Id", nullable = false)
public CaretakerDetails getCaretakerDetails() {
return this.caretakerDetails;
}
public void setCaretakerDetails(CaretakerDetails caretakerDetails) {
this.caretakerDetails = caretakerDetails;
}
@Column(name = "Device_Number", nullable = false)
public String getDeviceNumber() {
return this.deviceNumber;
}
public void setDeviceNumber(String deviceNumber) {
this.deviceNumber = deviceNumber;
}
@Column(name = "Created_Date", nullable = false)
public Date getCreatedDate() {
return this.createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
}
这里有人能告诉我这里做错了什么吗?我在这里错过了什么吗? 任何帮助将不胜感激!感谢
答案 0 :(得分:1)
您可以尝试将@GeneratedValue(strategy=GenerationType.AUTO)
代替@GeneratedValue(strategy = IDENTITY)
加入您的班级吗?