Hi Guys,
Been trying to solve the following issue for days. I hope anyone can shed some light on this.
My collections (child) are always deleted when i called saveOrUpdateCopy(parent) with/without any modifications.
I open up the session to load the parent and pass it back to the UI layer for some modifications (to both parent and the children.).
Somehow after session flush , the parent n children are updated but the children being deleted soon after.
Below are my sample codes:
<!-- MAPPING OF CHILD -->
<hibernate-mapping>
<class
name="Child"
table="CHILD"
>
<composite-id name="comp_id"
class="ChildPK">
<key-property
name="A"
column="A"
type="java.lang.String"
length="9"
/>
<key-property
name="B"
column="B"
type="java.lang.String"
length="1"
/>
<!-- bi-directional many-to-one association to Parent -->
<key-many-to-one
name="parent"
class="Parent"
>
<column name="PARENT_ID" />
</key-many-to-one>
</composite-id>
</class>
</hibernate-mapping>
<!-- MAPPING OF PARENT -->
<hibernate-mapping>
<class
name="Parent"
table="PARENT"
>
<id
name="parent_id"
type="java.lang.String"
column="PARENT_ID"
>
<generator class="assigned" />
</id>
<!-- Associations -->
<!-- bi-directional one-to-many association to Children -->
<set
name="child"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="PARENT_ID" />
</key>
<one-to-many
class="Child"
/>
</set>
</class>
</hibernate-mapping>
<!-- Parent Entity Class -->
/** @author Hibernate CodeGenerator */
public class Parent
implements Serializable {
private
String parentId;
/** persistent field */
private Set children;
public void addChild(Child child) {
if (child!= null) {
if (!children.contains(child)) {
child.setParent(this);
this.children.add(child);
}
}
}
public String toString() {
return new ToStringBuilder(this)
.append("parent_id", getParentId())
.toString();
}
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof Parent))return false;
Parent castOther = (Parent) other;
return new EqualsBuilder()
.append(this.getParentId(), castOther.getParentId())
.isEquals();
}
// Getters & Setters.....
}
/** @author Hibernate CodeGenerator */
public class Child
extends ChildPK
implements Serializable {
/** persistent field */
private String name;
// Getters and Setters.....
}
<!-- Child Entity Class -->
/** @author Hibernate CodeGenerator */
public class ChildPK implements Serializable {
/** identifier field */
private String a;
private String b;
/** identifier field */
private Parent parent
// Getters & Setters...
public String toString() {
return new ToStringBuilder(this)
.append("A", getA())
.append("B", getB())
.append("parent", getParent())
.toString();
}
public boolean equals(Object other) {
if (this == other) return true;
if (!(other instanceof ChildPK)) return false;
CsicDocApplcntPK castOther = (ChildPK) other;
return new EqualsBuilder()
.append(this.getA(), castOther.getA())
.append(this.getB(), castOther.getB())
.append(this.getParent(), castOther.getParent())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getA())
.append(getB())
.append(getParent())
.toHashCode();
}
}
<!-- Dao Class -->
Session session = getSession();
Object uncastedEntity = session.saveOrUpdateCopy(parent);
session.flush();
session.evict(uncastedEntity);
Please advise!!
Thanks!!