0

I have a table called conclusion and it has a relationship with table comments.

When i am adding conclusion and comments at the same time, Conclusion is being inserted but child getting conclusion id as null by which i'm getting violation of not null constraint. My transaction roll backs conclusion also not inserted.

So please tell me how to insert parent and child at once.

my entities are

Conclusion Entity

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

}

Comment Entity

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

}
udaykiran.nalla
  • 109
  • 1
  • 15

1 Answers1

2

The most probable reason for the join column being null in your table is that the olmAnlysCncln field is null in the OlmInvsgDiscussionComment entity you're trying to persist. That's the owner side of the bidirectional association. You MUST initialize it if you want Hibernate to set something in the corresponding column. Adding the comment to the conclusion is not sufficient.

Side note: vowels are accepted, and even recommented, in property names. You should be able to pronounce a property name. olmInvsgDiscnCmntAtmnts is unpronouceable and unreadable. Use proper English names.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • In the UI i'm trying to add conclusion and comment at the same time. I able to add comment only if my conclusion is persisted. To initialize olmAnlysCncln i don't have the conclusionId. Do you mean create a new object of OlmAnalysisConclusion and set this property to olm_anlys_cncln in OlmInvsgDiscussionComment? – udaykiran.nalla Feb 17 '14 at 08:54
  • You create the conclusion and persist it, then you create the comment, add it to the conclusion, set the conclusion to the one you've just persisted in the comment, and persist the comment. – JB Nizet Feb 17 '14 at 10:02
  • Same thing i'm doing now. which means i have to disable add comments until submission of new the conclusion. Is there any way to add both parent and child right away.. – udaykiran.nalla Feb 17 '14 at 10:09
  • Just as I told you. You start by creating and persisting the parent, then you create the child, set its parent, add it to the parent and persist it. You cn also add a cascade of type PERSIST on the association from parent to children. – JB Nizet Feb 17 '14 at 11:23
  • I have tried adding new comment to new conclusion its working fine. Last time forgot to give the reference of conclusion object to the comment. – udaykiran.nalla Feb 20 '14 at 11:21