java ee - Hibernate/JPA ManyToOne vs OneToMany -


i reading documentation of hibernate regarding entity associations , come accross little difficulty figure out things. has in essence difference between manytoone , onetomany associations. although have used them in real projects, cannot apprehend differnce between them. understanding, if table / entity has manytoone association another, association should other side onetomany. so, how should decide 1 choose based on specific case , how affect database/queries/results? there everywhere example?

p.s.: reckon helpful due relevance question, if besides explain point of owner of association , difference between bidirectional , unidirectional association.

suppose have order , orderline. can choose have unidirectional onetomany between order , orderline (order have collection of orderlines). or can choose have manytoone association between orderline , order (orderline have reference order). or can choose have both, in case association becomes bidirectional onetomany/manytoone association.

the solution choose depends on situation, , on level of coupling between entities. example, if user, company, provider have many addresses, make sense have unidirectional between every of them , address, , have address not know owner.

suppose have user , message, user can have thousands of messages, make sense model manytoone message user, because you'll ask messages of user anyway. association made bidirectional queries though, since jpql queries join between entities navigating through associations.

in bidirectional association, in situation graph of objects inconsistent. example, order have empty set of orderlines, orderlines have reference order a. jpa imposes have 1 side of association being owner side, , other side being inverse side. inverse side ignored jpa. owner side side decides relation exists. in onetomany bidirectional association, owner side must many side. so, in previous example, owner side orderline, , jpa persist association between lines , order a, since lines have reference a.

such association mapped this:

in order :

@onetomany(mappedby = "parentorder") // mappedby indicates side     // inverse side, , mapping defined attribute parentorder     // @ other side of association. private set<orderline> lines; 

in orderline :

@manytoone private order parentorder; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -