0

I have two classes CaretakerDetails and Devices there is a OneToMany relationship between the two entities but when I am trying to insert the object it is throwing the following exception:

Cannot insert the value NULL into column 'Caretaker_Id', table 'Emomtracking.dbo.Device_Details';

Caretaker Class is as follows:

@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;
}

}

Device entity is as follows:

  @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;
}

}

Can anyone here tell me what I am doing wrong here? Am I missing something here? Any help will be greatly appreciated! Thanks

2
  • Try setting the updateble to false : @Column(name = "Device_Id", updatable = false, nullable = false) Commented Jan 19, 2018 at 7:30
  • Still facing the same issue. Commented Jan 19, 2018 at 7:35

1 Answer 1

1

Could you try to put @GeneratedValue(strategy=GenerationType.AUTO) instead @GeneratedValue(strategy = IDENTITY) in your class ?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.