| Author |
JPA Many to Many relationship with attribute to self
|
Leonardo Shikida
Greenhorn
Joined: Jan 02, 2003
Posts: 28
|
|
How do I map this relationship using JPA?
TIA
Leo
|
 |
Angel Taveras
Ranch Hand
Joined: Nov 13, 2008
Posts: 84
|
|
Hello, Leonardo, could you post please at least the basic mapping that you're trying to map.
Regards,
Angel
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
And please UseRealWords. It took me 2 minutes at google to find out that TIA it means Thanks In Advance.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Leonardo Shikida
Greenhorn
Joined: Jan 02, 2003
Posts: 28
|
|
Let´s suppose I am trying to map something like this
- One section may contain several subsections
- Each subsection may belong to any set of sections
- A subsection is also a section
- We must keep the order these subsections are stored (the user must know which one is the first, which one is the second, etc)
- There is no limit to the hierarchy level
So it seems I can have a table like this
Section <--N---1--SectionRelationship--1----N-->Section
where SectionRelationship contains an attribute, let´s say, "order" (integer)
How do I map this into JPA?
TIA
Leo
|
 |
Karsten Wutzke
Ranch Hand
Joined: Jul 20, 2010
Posts: 102
|
|
We can't do the work for you.
It looks like you need two tables, one for the m:n links and one for the nodes. -> You are effectively creating a graph structure in a DB. There's actually nothing special about that. The techniques around that are sufficiently documented.
Karsten
|
OCJP JavaSE 6 (86%)
|
 |
Angel Taveras
Ranch Hand
Joined: Nov 13, 2008
Posts: 84
|
|
Leo, i think that this could be mapped as follows:
In JPA 1.0 you cannot sort the resulting List with an aditional column just with the columns provided in the target entity; with Hibernate annotation @OrderColumn(name="order") you could. I didn't understand when you write
- Each subsection may belong to any set of sections
that means a bidirectional relationship?
Regards,
Angel
|
 |
Leonardo Shikida
Greenhorn
Joined: Jan 02, 2003
Posts: 28
|
|
Hi Karsten. Well, I am not cutting and pasting my requirements and asking people to do my job. Sorry if you´ve thought I was doing this.
From this site - http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns - there is an example, so it seems I can´t use @ManyToMany directly. Instead, I am using two 1:N relationships
The problem is that the relationship is a self join, so I was a little bit confused about keeping 2 Lists in the same object to acomodate both relationships (yes, bi-directional).
So a little example would be nice, that´s what I am asking for.
TIA
Leo
|
 |
James Sutherland
Ranch Hand
Joined: Oct 01, 2007
Posts: 421
|
|
If you use JPA 2.0 you can just use an OrderColumn.
i.e.
See,
http://en.wikibooks.org/wiki/Java_Persistence/Relationships#Order_Column_.28JPA_2.0.29
|
TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
|
 |
 |
|
|
subject: JPA Many to Many relationship with attribute to self
|
|
|