HI -
I am new to hibernate and I spent hours but not able to find a proper solution. I am hoping I will get some hints from experts.
I am loading the parent from XML file and the child is loaded with empty set. Before persisting the parent I check if the parent already exists using the PARENT_ID and in my case it find that the parent exists and there is one existing child record so when I do a em.merge() I am expecting that hibernate should delete the existing child records as the set is empty in the current parent but instead it is doing an update query which is throwing a NOT NULL exception.
Update CHILD_TABLE set PARENT_ID=null where PARENT_ID=?
Following is my code snippet of parent / child
@Entity @Name("parent") @Scope(ScopeType.SESSION) @Table(name="Parent_Table") public class Parent implements Serializable {
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="tpGen") @SequenceGenerator(name="tpGen", sequenceName="TP_SEQ") @NotNull @Column(name="SEQ_ID") @Id public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; } @Column(name="PARENT_ID") public String getParentId() { return parentId; }
public void setParentId(String parentId) { this.parentId = parentId; } @OneToMany(targetEntity=Child.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL) @JoinColumn(name="PARENT_ID", referencedColumnName="PARENT_ID") @NotFound(action=NotFoundAction.IGNORE) @Cascade({org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.DELETE_ORPHAN}) public Set<Child> getChildSet() { return childSet; }
}
---------------------------------------------------------
Child Class -----------
@Entity @Name("Child") @Scope(ScopeType.SESSION) @Table(name="Child_Table") public class FileNamingTemplateMembership implements Serializable {
private transient String parentId; private String templateName; private transient String updateUserId; private transient Date updateDate; @Id @NotNull @Length(max=80) @Column(name="PARENT_ID") public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } @Length(max=40) @NotNull @Column(name="CHILD_NAME") public String getChildName() { return childName; } public void setChildName(String childName) { this.childName = childName; } @Length(max=80) @Column(name="UPDT_USER_ID") public String getUpdateUserId() { return updateUserId; } public void setUpdateUserId(String updateUserId) { this.updateUserId = updateUserId; } @Column(name="UPDT_DT") public Date getUpdateDate() { return updateDate; } public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; }
}
|