如何在JPA中坚持父母和孩子?

时间:2014-02-17 05:29:12

标签: java hibernate postgresql jpa

我有一个名为结论的表,它与表注释有关系。

当我同时添加结论和评论时,正在插入结论但是孩子得到的结论id为null,因为我违反了非空约束。 我的交易回滚结论也未插入。

所以请告诉我如何一次插入父母和孩子。

我的实体

结论实体

@Entity
@Table(name="olm_anlys_cncln")
public class OlmAnalysisConclusion implements Serializable {
private static final long serialVersionUID = 1L;
private Long conclusionId;
private String concludedBy;
private Timestamp concludedTime;
private String conclusion;
private Timestamp discussionEndTime;
private String discussionActive;
private Timestamp discussionStartTime;
private Integer tntId;
private OlmAnalysis olmAnly;
private OlmAnalysisCategory olmAnlysCatgMstr;
private OlmAnalysisCause olmAnlysCauseMstr;
private List<OlmInvsgDiscussionComment> olmInvsgDiscnCmnts;

public OlmAnalysisConclusion() {
}


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="olm_anlys_cncln_id")
public Long getConclusionId() {
    return this.conclusionId;
}

public void setConclusionId(Long conclusionId) {
    this.conclusionId = conclusionId;
}


@Column(name="cncld_by")
public String getConcludedBy() {
    return this.concludedBy;
}

public void setConcludedBy(String concludedBy) {
    this.concludedBy = concludedBy;
}


@Column(name="cncld_time")
public Timestamp getConcludedTime() {
    return this.concludedTime;
}

public void setConcludedTime(Timestamp concludedTime) {
    this.concludedTime = concludedTime;
}


@Column(name="cncln")
public String getConclusion() {
    return this.conclusion;
}

public void setConclusion(String conclusion) {
    this.conclusion = conclusion;
}


@Column(name="discn_end_time")
public Timestamp getDiscussionEndTime() {
    return this.discussionEndTime;
}

public void setDiscussionEndTime(Timestamp discussionEndTime) {
    this.discussionEndTime = discussionEndTime;
}


@Column(name="discn_flag")
public String getDiscussionActive() {
    return this.discussionActive;
}

public void setDiscussionActive(String discussionActive) {
    this.discussionActive = discussionActive;
}


@Column(name="discn_strt_time")
public Timestamp getDiscussionStartTime() {
    return this.discussionStartTime;
}

public void setDiscussionStartTime(Timestamp discussionStartTime) {
    this.discussionStartTime = discussionStartTime;
}


@Column(name="tnt_id")
public Integer getTntId() {
    return this.tntId;
}

public void setTntId(Integer tntId) {
    this.tntId = tntId;
}


//bi-directional many-to-one association to OlmAnalysis
@ManyToOne
@JoinColumn(name="anlys_id")
public OlmAnalysis getOlmAnly() {
    return this.olmAnly;
}

public void setOlmAnly(OlmAnalysis olmAnly) {
    this.olmAnly = olmAnly;
}


//bi-directional many-to-one association to OlmAnalysisCategory
@ManyToOne
@JoinColumn(name="catg_id")
public OlmAnalysisCategory getOlmAnlysCatgMstr() {
    return this.olmAnlysCatgMstr;
}

public void setOlmAnlysCatgMstr(OlmAnalysisCategory olmAnlysCatgMstr) {
    this.olmAnlysCatgMstr = olmAnlysCatgMstr;
}


//bi-directional many-to-one association to OlmAnalysisCause
@ManyToOne
@JoinColumn(name="cause_id")
public OlmAnalysisCause getOlmAnlysCauseMstr() {
    return this.olmAnlysCauseMstr;
}

public void setOlmAnlysCauseMstr(OlmAnalysisCause olmAnlysCauseMstr) {
    this.olmAnlysCauseMstr = olmAnlysCauseMstr;
}


//bi-directional many-to-one association to OlmInvsgDiscussionComment
@OneToMany(fetch=FetchType.EAGER,mappedBy="olmAnlysCncln",cascade=CascadeType.ALL)
public List<OlmInvsgDiscussionComment> getOlmInvsgDiscnCmnts() {
    return this.olmInvsgDiscnCmnts;
}

public void setOlmInvsgDiscnCmnts(List<OlmInvsgDiscussionComment> olmInvsgDiscnCmnts) {
    this.olmInvsgDiscnCmnts = olmInvsgDiscnCmnts;
}

public OlmInvsgDiscussionComment addOlmInvsgDiscnCmnt(OlmInvsgDiscussionComment olmInvsgDiscnCmnt) {
    getOlmInvsgDiscnCmnts().add(olmInvsgDiscnCmnt);
    olmInvsgDiscnCmnt.setOlmAnlysCncln(this);

    return olmInvsgDiscnCmnt;
}

public OlmInvsgDiscussionComment removeOlmInvsgDiscnCmnt(OlmInvsgDiscussionComment olmInvsgDiscnCmnt) {
    getOlmInvsgDiscnCmnts().remove(olmInvsgDiscnCmnt);
    olmInvsgDiscnCmnt.setOlmAnlysCncln(null);

    return olmInvsgDiscnCmnt;
}

}

评论实体

@Entity
@Table(name="olm_invsg_discn_cmnt")
public class OlmInvsgDiscussionComment implements Serializable {
private static final long serialVersionUID = 1L;
private Long commentId;
private String comment;
private Timestamp commentTime;
private String commentedBy;
private Integer tenentId;
private OlmAnalysisConclusion olmAnlysCncln;
private Set<OlmInvsgCommentAttachment> olmInvsgDiscnCmntAtmnts;

public OlmInvsgDiscussionComment() {
}


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="olm_invsg_discn_cmnt_id")
public Long getCommentId() {
    return this.commentId;
}

public void setCommentId(Long commentId) {
    this.commentId = commentId;
}


@Column(name="cmnt")
public String getComment() {
    return this.comment;
}

public void setComment(String comment) {
    this.comment = comment;
}


@Column(name="cmnt_time")
public Timestamp getCommentTime() {
    return this.commentTime;
}

public void setCommentTime(Timestamp commentTime) {
    this.commentTime = commentTime;
}


@Column(name="cmntd_by")
public String getCommentedBy() {
    return this.commentedBy;
}

public void setCommentedBy(String commentedBy) {
    this.commentedBy = commentedBy;
}


@Column(name="tnt_id")
public Integer getTenentId() {
    return this.tenentId;
}

public void setTenentId(Integer tenentId) {
    this.tenentId = tenentId;
}


//bi-directional many-to-one association to OlmAnalysisConclusion
@ManyToOne(fetch=FetchType.EAGER, optional=false)
@JoinColumn(name="cncln_id")
public OlmAnalysisConclusion getOlmAnlysCncln() {
    return this.olmAnlysCncln;
}

public void setOlmAnlysCncln(OlmAnalysisConclusion olmAnlysCncln) {
    this.olmAnlysCncln = olmAnlysCncln;
}


//bi-directional many-to-one association to OlmInvsgCommentAttachment
@OneToMany(fetch=FetchType.EAGER,mappedBy="olmInvsgDiscnCmnt",cascade=CascadeType.ALL)
public Set<OlmInvsgCommentAttachment> getOlmInvsgDiscnCmntAtmnts() {
    return this.olmInvsgDiscnCmntAtmnts;
}

public void setOlmInvsgDiscnCmntAtmnts(Set<OlmInvsgCommentAttachment> olmInvsgDiscnCmntAtmnts) {
    this.olmInvsgDiscnCmntAtmnts = olmInvsgDiscnCmntAtmnts;
}

public OlmInvsgCommentAttachment addOlmInvsgDiscnCmntAtmnt(OlmInvsgCommentAttachment olmInvsgDiscnCmntAtmnt) {
    getOlmInvsgDiscnCmntAtmnts().add(olmInvsgDiscnCmntAtmnt);
    olmInvsgDiscnCmntAtmnt.setOlmInvsgDiscnCmnt(this);

    return olmInvsgDiscnCmntAtmnt;
}

public OlmInvsgCommentAttachment   removeOlmInvsgDiscnCmntAtmnt(OlmInvsgCommentAttachment olmInvsgDiscnCmntAtmnt) {
    getOlmInvsgDiscnCmntAtmnts().remove(olmInvsgDiscnCmntAtmnt);
    olmInvsgDiscnCmntAtmnt.setOlmInvsgDiscnCmnt(null);

    return olmInvsgDiscnCmntAtmnt;
}

}

1 个答案:

答案 0 :(得分:2)

您的表中连接列为空的最可能原因是您尝试保留的olmAnlysCncln实体中的null字段为OlmInvsgDiscussionComment。这是双向关联的所有者方。如果希望Hibernate在相应的列中设置某些内容,则必须对其进行初始化。将评论添加到结论中是不够的。

旁注:在属性名称中接受,甚至推荐元音。您应该能够发音属性名称。 olmInvsgDiscnCmntAtmnts是不可理解且不可读的。使用正确的英文名称。