• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing double linked tree structure using JPA (sons-fathers)

 
Kenneth Gustafsson
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding tree structures and JPA. My example is an entierly male population where the nodes are HumanMan's which may or may not have sons.

In this structure it's convienient to have instance variables holding references to each HumanMan's father as well as his sons. That way you're able to traverse the tree in a fast and simple manner.

However! A HumanMan X's children are equal to all children having X as father. If it is possible with JPA I would like to use one table with two columns in the database.

At the moment I have two tables. I want one as the sons can be inferred from simply the son-father relationship created by father.

My current tables:



And I also have this one which isn't really necessary but JPA thinks so:


My current code:

Of course JPA can't know this relationship by itself (that children can be inferred using parents), but maybe there's some way of telling it so it doesn't create the humanman_humanman table?

My current line of thought is to make sons @Transient and use @PostLoad to fill it with the correct data. But isn't there any cleaner JPA'ish solution? Some annotation I missed perhaps?
[ April 09, 2008: Message edited by: Kent Larsson ]
 
wa rex
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same problem when storing taxonomies with JPA.

After some playing, I've found this (bad) solution:


Needless to say, notRealsons and notRealfather should be declared private but it is impossible because JPA won't allow it! Even setting these properties as private will make JPA to overwork for something that should be made simpler (IMHO).

Any ideas?

Originally posted by Kent Larsson:
I have a question regarding tree structures and JPA. My example is an entierly male population where the nodes are HumanMan's which may or may not have sons.

In this structure it's convienient to have instance variables holding references to each HumanMan's father as well as his sons. That way you're able to traverse the tree in a fast and simple manner.

However! A HumanMan X's children are equal to all children having X as father. If it is possible with JPA I would like to use one table with two columns in the database.

At the moment I have two tables. I want one as the sons can be inferred from simply the son-father relationship created by father.

My current tables:



And I also have this one which isn't really necessary but JPA thinks so:


My current code:

Of course JPA can't know this relationship by itself (that children can be inferred using parents), but maybe there's some way of telling it so it doesn't create the humanman_humanman table?

My current line of thought is to make sons @Transient and use @PostLoad to fill it with the correct data. But isn't there any cleaner JPA'ish solution? Some annotation I missed perhaps?

[ April 09, 2008: Message edited by: Kent Larsson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!