File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Parent-Child Mapping with Hibernate 2.0 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Parent-Child Mapping with Hibernate 2.0" Watch "Parent-Child Mapping with Hibernate 2.0" New topic
Author

Parent-Child Mapping with Hibernate 2.0

BC Gwee
Greenhorn

Joined: Nov 27, 2007
Posts: 1
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!!
Shailesh Kini
Ranch Hand

Joined: Oct 17, 2001
Posts: 153
Hello BC,

Any reason for evicting the entity from the session cache? Can you please make sure that parent.getChildren() actually has all the related children before you call saveOrUpdate on the session?


Shailesh Kini.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent-Child Mapping with Hibernate 2.0
 
Similar Threads
what changes should i made in save method in one-to-many relationships
One to many Relation
Hibernate Question
Hibernate 3.0 : Where am I going wrong?
Hibernate Question