我正在开发Web服务,其中有父子表的场景如下
**Parent Table**
@Entity
@Table(name = "INSTITUTE_LIST_MST")
@JsonIgnoreProperties(ignoreUnknown = true)
public class InstituteInfoMatster
{
@Id
@Column(name = "LIST_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer listId;
@Column(name = "LIST_DESC")
private String description;
@Column(name = "LIST_VALUE")
private String value;
@Column(name = "URL")
private String url;
@Column(name = "LOGO", unique = false, length = 100000)
private byte[] logo;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "instituteInfotMaster", cascade = CascadeType.ALL)
private List<InstituteInfoDetails> instituteInfoDetails = new ArrayList<InstituteInfoDetails>();
@Column(name = "CREATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate = new Date();
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "UPDATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDate;
@Column(name = "UPDATED_BY")
private String updatedBy;
@Column(name = "RECORD_STATUS")
private String recordStatus = "A";
public Integer getListId()
{
return listId;
}
public void setListId(Integer listId)
{
this.listId = listId;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public Date getCreatedDate()
{
return createdDate;
}
public void setCreatedDate(Date createdDate)
{
this.createdDate = createdDate;
}
public String getCreatedBy()
{
return createdBy;
}
public void setCreatedBy(String createdBy)
{
this.createdBy = createdBy;
}
public Date getUpdatedDate()
{
return updatedDate;
}
public void setUpdatedDate()
{
this.updatedDate = new Date();
}
public String getUpdatedBy()
{
return updatedBy;
}
public void setUpdatedBy(String updatedBy)
{
this.updatedBy = updatedBy;
}
public String getRecordStatus()
{
return recordStatus;
}
public void setActiveRecordStatus()
{
this.recordStatus = "A";
}
public void deleteRecord()
{
this.recordStatus = "D";
}
public List<InstituteInfoDetails> getInstituteInfoDetails()
{
return instituteInfoDetails;
}
public void setInstituteInfoDetails(List<InstituteInfoDetails> instituteInfoDetails)
{
this.instituteInfoDetails = instituteInfoDetails;
}
public byte[] getLogo()
{
return logo;
}
public void setLogo(byte[] logo)
{
this.logo = logo;
}
public String getUrl()
{
return url;
}
public void setUrl(String url)
{
this.url = url;
}
@Override
public String toString()
{
return "InstituteInfoMatster [listId=" + listId + ", description=" + description + ", value=" + value + ", url="
+ url + ", logo=" + Arrays.toString(logo) + ", instituteInfoDetails=" + instituteInfoDetails
+ ", createdDate=" + createdDate + ", createdBy=" + createdBy + ", updatedDate=" + updatedDate
+ ", updatedBy=" + updatedBy + ", recordStatus=" + recordStatus + "]";
}
}
子表
@Entity
@Table(name = "INSTITUTE_LIST_DETAILS")
public class InstituteInfoDetails
{
@Id
@Column(name = "LIST_DTL_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer listDtlId;
@ManyToOne
@JoinColumn(name = "LIST_ID", nullable = false)
@JsonProperty("instituteInfotMaster")
@JsonBackReference
private InstituteInfoMatster instituteInfotMaster;
@Column(name = "LIST_DTL_VALUE")
private String value;
@Column(name = "LIST_DTL_DESC", length = 5000)
private String description;
@Column(name = "STRING1", length = 5000)
private String string1;
@Column(name = "STRING2", length = 5000)
private String string2;
@Column(name = "STRING3", length = 5000)
private String string3;
@Column(name = "SEQUENCE_NO")
private int sequenceNo;
@Column(name = "NUMBER1")
private Double number1;
@Column(name = "NUMBER2")
private Double number2;
@Column(name = "NUMBER3")
private Double number3;
@Column(name = "DOCUMENT", unique = false, length = 100000)
private byte[] document;
@Column(name = "DOCUMENT_TYPE", length = 1)
private Integer documentType;
@Column(name = "DOCUMENT1", unique = false, length = 100000)
private byte[] document1;
@Column(name = "DOCUMENT1_TYPE", length = 1)
private Integer document1Type;
@Column(name = "DOCUMENT2", unique = false, length = 100000)
private byte[] document2;
@Column(name = "DOCUMENT2_TYPE", length = 1)
private Integer document2Type;
@Column(name = "CREATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate = new Date();
@Column(name = "CREATED_BY")
private String createdBy;
@Column(name = "UPDATED_DT")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedDate;
@Column(name = "UPDATED_BY")
private String updatedBy;
@Column(name = "RECORD_STATUS")
private String recordStatus = "A";
public Integer getListDtlId()
{
return listDtlId;
}
public void setListDtlId(Integer listDtlId)
{
this.listDtlId = listDtlId;
}
public String getValue()
{
return value;
}
public void setValue(String value)
{
this.value = value;
}
public String getDescription()
{
return description;
}
public void setDescription(String description)
{
this.description = description;
}
public InstituteInfoMatster getComListMaster()
{
return instituteInfotMaster;
}
public void setComListMaster(InstituteInfoMatster instituteInfotMaster)
{
this.instituteInfotMaster = instituteInfotMaster;
}
public Date getCreatedDate()
{
return createdDate;
}
public void setCreatedDate()
{
this.createdDate = new Date();
}
public String getCreatedBy()
{
return createdBy;
}
public void setCreatedBy(String createdBy)
{
this.createdBy = createdBy;
}
public Date getUpdatedDate()
{
return updatedDate;
}
public void setUpdatedDate(Date updatedDate)
{
this.updatedDate = updatedDate;
}
public String getUpdatedBy()
{
return updatedBy;
}
public void setUpdatedBy(String updatedBy)
{
this.updatedBy = updatedBy;
}
public String getRecordStatus()
{
return recordStatus;
}
public void setRecordStatus(String recordStatus)
{
this.recordStatus = recordStatus;
}
public InstituteInfoMatster getInstituteInfotMaster()
{
return instituteInfotMaster;
}
public void setInstituteInfotMaster(InstituteInfoMatster instituteInfotMaster)
{
this.instituteInfotMaster = instituteInfotMaster;
}
public String getString1()
{
return string1;
}
public void setString1(String string1)
{
this.string1 = string1;
}
public String getString2()
{
return string2;
}
public void setString2(String string2)
{
this.string2 = string2;
}
public String getString3()
{
return string3;
}
public void setString3(String string3)
{
this.string3 = string3;
}
public int getSequenceNo()
{
return sequenceNo;
}
public void setSequenceNo(int sequenceNo)
{
this.sequenceNo = sequenceNo;
}
public Double getNumber1()
{
return number1;
}
public void setNumber1(Double number1)
{
this.number1 = number1;
}
public Double getNumber2()
{
return number2;
}
public void setNumber2(Double number2)
{
this.number2 = number2;
}
public Double getNumber3()
{
return number3;
}
public void setNumber3(Double number3)
{
this.number3 = number3;
}
public byte[] getDocument()
{
return document;
}
public void setDocument(byte[] document)
{
this.document = document;
}
public Integer getDocumentType()
{
return documentType;
}
public void setDocumentType(Integer documentType)
{
this.documentType = documentType;
}
public byte[] getDocument1()
{
return document1;
}
public void setDocument1(byte[] document1)
{
this.document1 = document1;
}
public Integer getDocument1Type()
{
return document1Type;
}
public void setDocument1Type(Integer document1Type)
{
this.document1Type = document1Type;
}
public byte[] getDocument2()
{
return document2;
}
public void setDocument2(byte[] document2)
{
this.document2 = document2;
}
public Integer getDocument2Type()
{
return document2Type;
}
public void setDocument2Type(Integer document2Type)
{
this.document2Type = document2Type;
}
@Override
public String toString()
{
return "InstituteInfoDetails [listDtlId=" + listDtlId + ", instituteInfotMaster=" + instituteInfotMaster
+ ", value=" + value + ", description=" + description + ", string1=" + string1 + ", string2=" + string2
+ ", string3=" + string3 + ", sequenceNo=" + sequenceNo + ", number1=" + number1 + ", number2="
+ number2 + ", number3=" + number3 + ", document=" + Arrays.toString(document) + ", documentType="
+ documentType + ", document1=" + Arrays.toString(document1) + ", document1Type=" + document1Type
+ ", document2=" + Arrays.toString(document2) + ", document2Type=" + document2Type + ", createdDate="
+ createdDate + ", createdBy=" + createdBy + ", updatedDate=" + updatedDate + ", updatedBy=" + updatedBy
+ ", recordStatus=" + recordStatus + "]";
}
}
现在我正在使用以下休息服务请求添加包含子项的主数据
{"description":"Placements","value":"Placements","url":"/Placements","instituteInfoDetails":[{"value":"Test"}]}
保存数据的代码
@Override
public void addInstituteInfoMaster(com.zertones.model.common.InstituteInfoMatster instituteInfoMatster)
{
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(instituteInfoMatster);
}
但保存时会给我以下错误
org.hibernate.PropertyValueException: not-null property references a null or transient value : com.zertones.model.common.InstituteInfoDetails.instituteInfotMaster
我搜索并根据解决方案做了我添加了级联类型信息,但它没有帮助。
答案 0 :(得分:0)
我认为你在这段代码中有一个拼写错误:
@ManyToOne
@JoinColumn(name = "LIST_ID", nullable = false)
@JsonProperty("instituteInfotMaster")
@JsonBackReference
private InstituteInfoMatster instituteInfotMaster;
不应该是instituteInfoMaster
吗?
InstituteInfoMatster
等等。不过,我认为在你还可以的时候解决这些拼写错误是值得的(如果你还可以,当然)。
答案 1 :(得分:0)
创建用于将InstituteInfoMatster设置为instituteInfoDetails
的双向Setter方法更改类InstituteInfoMatster
public void setInstituteInfoDetails(List<InstituteInfoDetails> instituteInfoDetails)
{
for(InstituteInfoDetails ins : instituteInfoDetails){
ins.setComListMaster(this);
}
this.instituteInfoDetails = instituteInfoDetails;
}