• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tree structure in Hibernate

 
Bob Damiano
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm learning hibernate and thought I'd do a simple task management application as a bit of study practice.

I have a class sort of like so:



A database table sort of like so:
id
parent_task_id
...other properties...

And lastly a xml mapping sort of like so:


Notice the collection association is with other rows in the same table instead of another table as in most of the parent-child examples I've seen. The idea is that a task can have many tasks as children, and so on.

So, when I load up my list of tasks, the parent/child relationships are perfect. I have a custom tag that renders them recursively with links to expand/collapse tasks. Good so far.

Then I try to edit a task and the trouble starts. If I update a task that has no children, all is good. The hibernate traces indicate a single update and the row really updates. If I try to update a task that has children, I can see in the hibernate log traces that hibernate is issuing something like:

update task set title=?, ... other properties ... where id=? (Looks good to me)

followed by:
update task set parent_task_id=null, id=null where parent_task_id=?

Not good. This fails because of course id and parent id are not-null in the schema. What I don't understand is why it would be attempting to update any record's parent_task_id or id. And further why it's just setting to literal null rather than an argument placeholder.

I guess my questions are... can someone give me a bit of an explanation of why this second update happens when there are associated children. Is there a way to avoid this problem with my current design? Is the design totally stupid and inefficient and should be scrapped anyway?

Thanks in advance for any help.

cheers,
-Bob
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic