• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Linwood Hayes
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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.
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2536
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Himai Minh
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • 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
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1361
7
  • Mark post as helpful
  • send pies
  • 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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic