2015-09-06 64 views
1

我开发Web服务,以节省子表的数据在此有父子表的情况下,如下无法使用Hibernate

**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

创建组InstituteInfoMatster到instituteInfoDetails双向setter方法

变化类InstituteInfoMatster

public void setInstituteInfoDetails(List<InstituteInfoDetails> instituteInfoDetails) 
{ 
    for(InstituteInfoDetails ins : instituteInfoDetails){ 
     ins.setComListMaster(this); 
    } 
    this.instituteInfoDetails = instituteInfoDetails; 
} 
0

我觉得你有这个代码一个错字:

@ManyToOne 
@JoinColumn(name = "LIST_ID", nullable = false) 
@JsonProperty("instituteInfotMaster") 
@JsonBackReference 
private InstituteInfoMatster instituteInfotMaster; 

不是应该instituteInfoMaster呢?编辑:或者可能不是,我可以说你有多个错别字,例如InstituteInfoMatster等等。不过,我认为在你仍然可以(如果你仍然可以的话)的情况下修复这些错别字是值得的。

相关问题