Hibernate Help Needed
I'll try the approach that Lance used to solve my Hibernate problem. First, I'll explain the problem. In two days, if I haven't solved it - I'll bash on Hibernate and say it s***s (can't do it yet - right ;-), and then I'll claim it rocks once I get my problem solved. Hmmm, it took Lance 4 days - I have 3 before my deadline (Thursday). Luckily, I know of workarounds already (I just want to do things the recommended way) - therefore, I should never have to bash on Hibernate.
My problem is basically that I have a Parent object and it has a one-to-many relationship to a Child object. This is usually a very simple thing to configure in a Parent.hbm.xml file. To make things complicated, my Child object has a composite-id which is another class. I can retrieve information just fine, but when I try to save the Parent, Hibernate tries to insert a new Child. Here's the message I sent to the hibernate-devel mailing list:
** all classes extend from BaseObject with has equals(),
hashCode() and toString() methods.
Parent.java
---
private Long id;
private List children;
+appropriate getters/setters
Parent.hbm.xml
---
<bag role="children" table="CHILDREN" cascade="all">
<key column="parent_id" length="22"/>
<one-to-many class="eg.Child"/>
</bag>
Child.java
---
private ChildId id;
+appropriate getter/setter
ChildId.java
---
private Long parentId;
private Long recordNum;
+ appropriate getter/setters
Child.hbm.xml
---
<composite-id name="id"
class="eg.ChildId"
unsaved-value="none">
<key-property column="parent_id" length="22"
name="parentId" type="long"/>
<key-property column="record_num" length="22"
name="recordNum" type="long"/>
</composite-id>
I am able to retrieve children just fine, but when I try to save
the parent, hibernate tries to do an insert and I get a unique
constraint violation.
I'm calling the following method to save everything:
public Parent saveParent(Parent p) throws DAOException {
storeObject(p);
return (Parent) retrieveObject(eg.Parent.class, p.getId());
}
I got the storeObject method from one of the examples floating
around, so it could definitely be my problem:
protected void storeObject(Object obj) throws DAOException {
Session ses = null;
try {
ses = HibernateSession.currentSession();
ses.saveOrUpdate(obj);
ses.flush();
ses.connection().commit();
} catch (Exception e) {
try {
ses.connection().rollback();
} catch (Exception ex) {
e.printStackTrace();
}
;
throw new DAOException(e);
} finally {
try {
HibernateSession.closeSession();
} catch (Exception ex) {
ex.printStackTrace();
}
;
}
}
As always, any help is greatly appreciated.


