gpt4 book ai didi

java - Hibernate 尝试在外键列中插入 null

转载 作者:行者123 更新时间:2023-12-02 11:43:57 25 4
gpt4 key购买 nike

我有两个类 CaretakerDetails 和 Devices,这两个实体之间存在 OneToMany 关系,但是当我尝试插入对象时,它抛出以下异常:

Cannot insert the value NULL into column 'Caretaker_Id', table '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;
}

}

这里有人可以告诉我我在这里做错了什么吗?我在这里错过了什么吗?任何帮助将不胜感激!谢谢

最佳答案

您可以尝试将 @GenerateValue(strategy=GenerationType.AUTO) 代替 @GenerateValue(strategy = IDENTITY) 放入您的类中吗?

关于java - Hibernate 尝试在外键列中插入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48335927/

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