• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@ManyToOne versus @OneToMany, confused!

 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Hibernate guru's
Now at Chapter 18 in the tutorial of McKenzie, things start to become a bit confusing
I do not really get the point ofthose two differen annotations.
LEts start with th beginning, in the example we have 2 classes: Team and Player, in a 1:N relationship, a Team can have many Players (but apparently a Player can only be a member of one Team).
This pretty logically maps to 2 DB tables in a 1:N relationship:
Table Team:
id (PK)
name
city
...

Table Player:
id (PK)
first_name
last_name
...
team_id (FK)

So the table on the N-part of the 1:N relationship holds a foreign key reference to the table on th 1-part. So far so good.
AFAIK this is annotated in Hibernate using a @ManyToOne annotation on the getTeam() method in the Player class. An optional @JoinColumn annotation can be used to customize the name of the foreign key field (default named team_id)

This is of course a unidirectional association. The difference between undirecional and birectional associations does only make sense for the object model and has no meaning for the relational model.
-Does that mean that the @OneToMany annotation only is used for adding navigability from a Team object to its Players, thereby making it a bidirectional relationship?

-And if so, does that mean that the onyl value of this that I use the "List getPlayers() " method to view the Players of a Team?

-What happens if I save a Team object with an empty players List, but with some player objetcs around referring to the Team object? Or is the players List only valuable for navigating after reading in a Team object from the database?

So pretty confusing stuff this! It appears that the @MAnyToOne association is necessary to implement the 1:N relationship, and the @ONeToMany can optioanlly be used to faciliate navigation in the 1 --> N direction.
Any discussion or hints are very welcome


 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Klaas van Gelder wrote:I do not really get the point ofthose two differen annotations.


An example of where you might use a OneToMany relationship is with an Invoice object and its LineItem objects. The invoice object needs a list of all of the line items but the line items do not need to point back the invoice.

An exmaple of there you might use a ManyToOne relationship is with an Invoice object and a Customer object. A customer may have many invoices but the Custome object does not need to have a list of all invoices. However, the invoice, which is part of the many, has only one customer and needs to aggregate the Customer object.

Ran
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In ORM mapping, one to many and many to one are interconnected to each other. As given example of Invoice and line items, if you see from invoice side, it is one to many mapping. If you see from item side, it is many to one mapping. So both comes same time only difference is point of reference.

From the code point also, both are same. In both the case, the owner entity will be the entity from Many side. For example, One invoice contains multiple items. So only way to do database mapping is that Item table will contain one foreign key which will be invoice id ( primary key of invoice table).

So you mapping will contain @manyToOne in item entity. If you want to do bi-directional mapping, then you need to use
@OneToMany annotation in Invoice entity. This one to many has no effect on database mapping. It will be used only for bi-directional mapping.

Please let me know, if you have any query on this.

-Sunil
 
Klaas van Gelder
Ranch Hand
Posts: 111
Java Linux PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
xsunil kumar wrote:
So you mapping will contain @manyToOne in item entity. If you want to do bi-directional mapping, then you need to use
@OneToMany annotation in Invoice entity. This one to many has no effect on database mapping. It will be used only for bi-directional mapping.

Please let me know, if you have any query on this.

-Sunil

OK I think that confirms my assumptions. Does this mean that the @ManyToOne is necessary to generate the right SQL code for creating the foreign key?
And does this mean that @OneToMany has in fact no effect on the DB scheme as you already state?
What is exactly the effect of the @OneToMAny? Does this take care for populating the Collection object on the one site with the objects on the many site, making it only needed for read actions?
One more question about the Team-Player example of the tutorial of McKEnzie: the players member variable in the Team class is never initialized (normally this would happen in the constructor: players = new ArrayList() or something like that...).
So adding a PLayer to a Team is done by creating a new Player and assign the team back-reference variable of the Player class.
But how is the player collection variable of the Team class assigned? Does Hibernate take care of that? And is that the purpose of the @OneToMAny annotation?
Still lost a bit about this subject...
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, @ManyToOne is necessary to generate the object mapping in database. Hibernate/ JPA will take care of generating respective database sql scripts to fetch / insert data in mapped tables.

@OneToMany has no impact on DB schema. Its used only for Bi-diectional mapping. You can get more information on Bi-diectional mapping by googling.

I am not clear about your team- player question. Please explain more.

-Sunil
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic