Joined: Tue Jan 11, 2005 2:26 pm Posts: 15
|
Hi,
I was having a problem persisting a new child object using Hibernate 3.0.5 and MySql.
I have two object TrainingTask and TrainingTaskRevision. A TrainingTask has many revisions.
TrainingTask had a set of revisions and the revision had a one-many relationship back to the task.
I mapped the TrainingTask with the inverse="true" attribute.
The mappings were fine because I could retrieve the objects. I just couldn't persist a new one by just saving the parent (TrainingTask).
My code was as follows:
// create the revision
TrainingTaskRevision revision = new TrainingTaskRevision();
// populate it's properties
// set the relationships
revision.setTask(task);
task.getRevisions().add(revision);
taskDao.save(task);
That didn't work.
All I had to do to fix this issue was add:
taskDao.save(revision);
taskDao.save(task);
***NOTE: This is a legacy database without any foreign keys between the two tables.
Real question: Does hibernate need/use the foreign keys in order to persist a child?
Here are my mapping tags:
** NOTE above classes were shortened.
<!-- Mapping inside Task object -->
<set name="taskRevisions" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="task_id" />
<one-to-many class="com.qslabs.skills.impl.TrainingTaskRevisionDb" />
</set>
<!-- mapping inside TaskRevision object -->
<many-to-one
name="trainingTask"
column="task_id"
not-null="true"
lazy="true"
class="com.qslabs.skills.impl.TrainingTaskDb" />
|
|