• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parent-Child Mapping with Hibernate 2.0

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic