• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is it true that mappedBy is used in different way for one-to-many and many-to-many bidirectional ?

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read lot of samples from internet and books.  I found it seems the way to tell "who is the owner side of a bidirectional relation ?" is opposite for one-to-many and many-to-many.

1) For one-to-many





In this one-to-many case, "Publisher" is the owner and its class has the "mappedBy" setting.

2) many-to-many:





In this many-to-many case, "Group" is the owner of bidirection relation, and the "mappedBy" is set in the non owner class(User).

So, for bidirectional one-to-many and many-to-many "mappedBy" is put in opposite ways (one is put on owner side class and the other is on non owner side class).  

IS THIS TRUE ?

Note: The above 2 examples were all taken from some tutorial examples so I assume they are correct..
Thanks.
 
Creator of Enthuware JWS+ V6
Posts: 3411
320
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So, for bidirectional one-to-many and many-to-many "mappedBy" is put in opposite ways (one is put on owner side class and the other is on non owner side class).  

IS THIS TRUE ?


No, the mappedBy element is only specified on the inverse (non-owning) side of the association.

the specifications say:


- The many side of one-to-many / many-to-one bidirectional relationships must be the owning side, hence the mappedBy element cannot be specified on the ManyToOne annotation.
- For many-to-many bidirectional relationships either side may be the owning side.

 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of many to many bidirectional relationship, one side is designated as the owner while another side is designated as being owned.
It is designed by the database architect.

In your example,  

I think the group is the owner and users is the one being owned by the group.
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to p.85 of JPA Pro 2nd edition,


1. The many-to-one side should be the owning side, so the join column should be defined on that side.
2. The one-to-many mapping should be the inverse side, so the mappedBy element should be used.

 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to p.86-87 of JPA Pro 2nd edition, regarding to many-to-many bidirectional relationship:


...there is no way to determine which side is the owner of the relationship. Because every bi-directional relationship has to have both an owning side and inverse side.
We must pick one of the two entities to be the owner. In this example, we picked Employee to be owner of the relationship, but we could have just as easily picked Project instead.....





Note that no matter which side is designated as the owner, the other side should include the mappedBy element; otherwise the provider will think that both sides are the owner and the mappings are separate
unidirectional relationships.



For example, in chapter 4 of many-to-many relationship, it chooses Employee as the owner and the project as the non-owner:




 
Linwood Hayes
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK.  For one-to-many (In my case, Publisher-to-Books where "Publisher" is ONE and "Books" are many, i.e. one publisher can many books),  Since you guys say "many" side is the owner, "Book" side is the owner.  And I have made Publisher to include Book and Book to include Publisher (bidirectional one-to-many).   Now, when I save, is it proper to do

Publisher p = new Publisher(xxx);
Book book = new Book(nnn);
p.addBooks(book);
DAO.save(p);

to save publisher "p" and its associated books ?

If it is proper to do that then we are saving from the inverse side (publisher side) instead of owner side ...
 
Himai Minh
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use something like :



Then, if you add the book to the publisher , the persistence context will add commit this change to the DB.
For example, something like this:


The book_publisher join table will have the primary keys of the book and publisher like this (100, 1).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic