Since the table generated in the db are all the same,so the only difference I found is that each side of the bidiretional assocations will have a refer to the other,and the unidirectional not.

Đang xem: Unidirectional là gì, nghĩa của từ unidirectional

This is a Unidirectional association

public class User { private int id; private String name;
JoinColumn( name = “groupId”) private Group group;}public class Group { private int id; private String name;}The Bidirectional association

public class User { private int id; private String name;
JoinColumn( name = “groupId”) private Group group;}public class Group { private int id; private String name;
OneToMany(mappedBy=”group”) private List users;}The difference is whether the group holds a reference of the user.

So I wonder if this is the only difference? which is recommended?

*

*

The main differenece is that bidirectional relationship provides navigational access in both directions, so that you can access the other side without explicit queries. Also it allows you to apply cascading options to both directions.

Note that navigational access is not always good, especially for “one-to-very-many” and “many-to-very-many” relationships. Imagine a Group that contains thousands of Users:

How would you add new Users to the Group? Fortunately, Hibernate looks at the owning side of relationship when persisting it, so you can only set User.group. However, if you want to keep objects in memory consistent, you also need to add User to Group.users. But it would make Hibernate to fetch all elements of Group.users from the database!

So, I can”t agree with the recommendation from the Best Practices. You need to design bidirectional relationships carefully, considering use cases (do you need navigational access in both directions?) and possible performance implications.

See also:

Share
Improve this answer
Follow
edited Oct 10 “20 at 3:32

*

PerkOne
4311 silver badge66 bronze badges
answered Mar 19 “11 at 10:40

*

axtavtaxtavt
228k3737 gold badges489489 silver badges472472 bronze badges
7
| Show 2 more comments
38
There are two main differences.

Accessing the association sides

The first one is related to how you will access the relationship. For a unidirectional association, you can navigate the association from one end only.

So, for a unidirectional
ManyToOne association, it means you can only access the relationship from the child side where the foreign key resides.

If you have a unidirectional
OneToMany association, it means you can only access the relationship from the parent side which manages the foreign key.

For the bidirectional
OneToMany association, you can navigate the association in both ways, either from the parent or from the child side.

You also need to use add/remove utility methods for bidirectional associations to make sure that both sides are properly synchronized.

Xem thêm: Chùm Ngây Là Cây Gì ? Hình Ảnh, Tác Dụng, Cách Sử Dụng Chùm Cây Chùm Ngây Chữa Bệnh Gì

Performance

The second aspect is related to performance.

Share
Improve this answer
Follow
edited Oct 13 “20 at 17:02
answered Feb 8 “18 at 9:00

*

Vlad MihalceaVlad Mihalcea
102k3838 gold badges428428 silver badges783783 bronze badges
2
Add a comment |
11
In terms of coding, a bidirectional relationship is more complex to implement because the application is responsible for keeping both sides in synch according to JPA specification 5 (on page 42). Unfortunately the example given in the specification does not give more details, so it does not give an idea of the level of complexity.

When not using a second level cache it is usually not a problem to do not have the relationship methods correctly implemented because the instances get discarded at the end of the transaction.

When using second level cache, if anything gets corrupted because of wrongly implemented relationship handling methods, this means that other transactions will also see the corrupted elements (the second level cache is global).

A correctly implemented bi-directional relationship can make queries and the code simpler, but should not be used if it does not really make sense in terms of business logic.

Share
Improve this answer
Follow
answered Jul 26 “13 at 19:54
Constantino CronembergerConstantino Cronemberger
1,7902020 silver badges2828 bronze badges
Add a comment |
9
I”m not 100% sure this is the only difference, but it is the main difference. It is also recommended to have bi-directional associations by the Hibernate docs:

http://docs.jboss.org/hibernate/core/3.3/reference/en/html/best-practices.html

Specifically:

Prefer bidirectional associations: Unidirectional associations are more difficult to query. In a large application, almost all associations must be navigable in both directions in queries.

I personally have a slight problem with this blanket recommendation — it seems to me there are cases where a child doesn”t have any practical reason to know about its parent (e.g., why does an order item need to know about the order it is associated with?), but I do see value in it a reasonable portion of the time as well. And since the bi-directionality doesn”t really hurt anything, I don”t find it too objectionable to adhere to.

Share
Improve this answer
Follow
answered Mar 19 “11 at 8:03
kvistakvista
4,95211 gold badge2121 silver badges2424 bronze badges
1
Add a comment |

Your Answer

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Xem thêm:

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Submit

Post as a guest

Name
Email Required, but never shown

Post as a guest

Name
Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged java hibernate jpa orm associations or ask your own question.

The Overflow Blog
Featured on Meta
Visit chat
Linked
3
Unidirectional or bidirectional relationships in JPA?
12
Should
JoinTable be specified in both sides of a
ManyToMany relationship?
9
MappedBy in bi-directional
ManyToMany : what is the reason
4
Why is it necessary to distinguish one side of a bidirectional relationship in orm as an owning side?
3
Conditional/composite JoinColumn In JPA/Hibernate
2
Insert data into relationship table (many-to-many) with Spring boot
2
best practice using uni/bi assoications in jpa/hibernate
0
mapping a entity which have two refer to another same entity
2
Update generated after insert when bidirectional one to many mapping
0
Is it ok to save the parent if I am just adding a new child, java hibernate spring?
See more linked questions
Related
3950
What are the differences between a HashMap and a Hashtable in Java?
3343
What is the difference between public, protected, package-private and private in Java?
1637
Difference between StringBuilder and StringBuffer
1150
What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do
986
What is the difference between JDK and JRE?
170
What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association?
2261
What's the difference between
Component,
Repository &
Service annotations in Spring?
557
JPA JoinColumn vs mappedBy
837
What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
Hot Network Questions more hot questions

Question feed
Subscribe to RSS
Question feed To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-java
Stack Overflow
Products
Company
Stack Exchange Network
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev2021.5.11.39272

Stack Overflow works best with JavaScript enabled

*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *