【问题标题】:How to persist parent and childs in JPA?如何在JPA中坚持父母和孩子?
【发布时间】:2014-03-16 07:08:16
【问题描述】:

我有一个叫作结论的表,它与表 cmets 有关系。

当我同时添加结论和 cmets 时,正在插入结论,但孩子得到结论 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;
}

}

【问题讨论】:

标签: java hibernate postgresql jpa


【解决方案1】:

连接列在您的表中为空的最可能原因是olmAnlysCncln 字段是您尝试保留的OlmInvsgDiscussionComment 实体中的null。那是双向关联的所有者方。如果你想让 Hibernate 在相应的列中设置一些东西,你必须初始化它。在结论中添加评论是不够的。

旁注:元音在属性名称中被接受,甚至被推荐。您应该能够发音属性名称。 olmInvsgDiscnCmntAtmnts 无法发音且无法阅读。使用正确的英文名称。

【讨论】:

  • 在 UI 中我试图同时添加结论和评论。只有坚持我的结论,我才能添加评论。要初始化 olmAnlysCncln,我没有结论 ID。你的意思是创建一个新的OlmAnalysisConclusion对象并在OlmInvsgDiscussionComment中将此属性设置为olm_anlys_cncln?
  • 您创建结论并将其持久化,然后创建评论,将其添加到结论中,将结论设置为您刚刚在评论中持久化的结论,然后持久化评论。跨度>
  • 我现在正在做同样的事情。这意味着我必须禁用添加 cmets,直到提交新的结论。有什么方法可以立即添加父母和孩子..
  • 就像我告诉你的那样。您首先创建和持久化父级,然后创建子级,设置其父级,将其添加到父级并持久化。您还可以在从父级到子级的关联上添加一个 PERSIST 类型的级联。
  • 我已经尝试为新结论添加新评论,它工作正常。上次忘记在评论中给出结论对象的引用。
猜你喜欢
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多