• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA Many to Many relationship with attribute to self

 
Leonardo Shikida
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I map this relationship using JPA?

TIA

Leo
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Leonardo, could you post please at least the basic mapping that you're trying to map.

Regards,

Angel
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseRealWords. It took me 2 minutes at google to find out that TIA it means Thanks In Advance.
 
Leonardo Shikida
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic