This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

JPA Many to Many relationship with attribute to self  RSS feed

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

TIA

Leo
 
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
 
Bartender
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
 
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





 
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!