Hi,
I have a bidirectional parent child relation with an indexed collection. My mapping is as follows:
Code:
<hibernate-mapping>
<class name="foo.model.Task" table="tasks">
<id name="id">
<generator class="native" />
</id>
<version name="version" access="field" />
<many-to-one name="parent" column="project_id" class="foo.model.Project" insert="false" update="false" not-null="true" />
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="foo.model.Project" table="projects">
<id name="id">
<generator class="native" />
</id>
<version name="version" access="field" />
<list name="tasks" cascade="all-delete-orphan" inverse="false" access="field">
<key column="project_id" not-null="true" />
<list-index column="ordr" />
<one-to-many class="foo.model.Task" />
</list>
</class>
</hibernate-mapping>
I have also provided the necessary helper methods within my model:
Code:
public void addTask(Task task) {
task.setParent(this);
tasks.add(task);
}
public void removeTask(Task task) {
task.setParent(null);
tasks.remove(task);
}
I want to change the parent of the child and assign it to another. Something like this:
Code:
Task task = getTaskFromSomewhere();
initialParent.removeTask(task);
newParent.addTask(task);
This snippet seems to perform two queries. One delete to remove the task form the first parent's collection and then an insert to add it to the new one. Seems a bit of reduntant to me since there already is a record for it in the database so an update would ne fine. I understand that since I have mapped the many-to-one part of the relation with insert="false" and update="false" is the reason why I am not getting the update I am looking for. Is there a workaround for it or I just have to compromise with the delete and insert?
Thank you in advance,
Argyro