Unidirectional là gì, nghĩa của từ unidirectional


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 lớn the other,và the unidirectional not.

Bạn đ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 lớn 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 lớn 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 kiến thiết bidirectional relationships carefully, considering use cases (vì chưng you need navigational access in both directions?) & possible performance implications.

See also:

Improve this answer
edited Oct 10 "đôi mươi at 3:32

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

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

Accessing the association sides

The first one is related to lớn 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 sầu 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ì


The second aspect is related lớn performance.

Improve this answer
edited Oct 13 "trăng tròn at 17:02
answered Feb 8 "18 at 9:00

Vlad MihalceaVlad Mihalcea
102k3838 gold badges428428 silver badges783783 bronze badges
Add a bình luận |
In terms of coding, a bidirectional relationship is more complex khổng lồ implement because the application is responsible for keeping both sides in synch according to lớn 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 cấp độ of complexity.

When not using a second level cađậy it is usually not a problem khổng lồ vị not have sầu the relationship methods correctly implemented because the instances get discarded at the kết thúc of the transaction.

When using second màn chơi 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 cađậy is global).

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

Improve this answer
answered Jul 26 "13 at 19:54
Constantino CronembergerConstantino Cronemberger
1,7902020 silver badges2828 bronze badges
Add a phản hồi |
I"m not 100% sure this is the only difference, but it is the main difference. It is also recommended khổng lồ have sầu bi-directional associations by the Hibernate docs:



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 lớn me there are cases where a child doesn"t have sầu any practical reason to know about its parent (e.g., why does an order thành tích need khổng lồ know about the order it is associated with?), but I vày 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 khổng lồ adhere khổng lồ.

Improve sầu this answer
answered Mar 19 "11 at 8:03
4,95211 gold badge2121 silver badges2424 bronze badges
Add a phản hồi |

Your Answer

Thanks for contributing an answer to Staông xã Overflow!

Please be sure khổng lồ answer the question. Provide details và nói qua your research!

But avoid

Asking for help, clarification, or responding khổng lồ 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.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Facebook
Sign up using Email & Password

Post as a guest

Email Required, but never shown

Post as a guest


Required, but never shown

Post Your Answer Disthẻ

By clicking “Post Your Answer”, you agree to lớn 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
Unidirectional or bidirectional relationships in JPA?
JoinTable be specified in both sides of a
ManyToMany relationship?
MappedBy in bi-directional
ManyToMany : what is the reason
Why is it necessary lớn distinguish one side of a bidirectional relationship in orm as an owning side?
Conditional/composite JoinColumn In JPA/Hibernate
Insert data into lớn relationship table (many-to-many) with Spring boot
best practice using uni/bi assoications in jpa/hibernate
mapping a entity which have two refer khổng lồ another same entity
Update generated after insert when bidirectional one khổng lồ many mapping
Is it ok khổng lồ save sầu the parent if I am just adding a new child, java hibernate spring?
See more linked questions
What are the differences between a HashMap and a Hashtable in Java?
What is the difference between public, protected, package-private and private in Java?
Difference between StringBuilder & StringBuffer
What are the possible values of the Hibernate hbm2ddl.auto configuration & what vì chưng they vì chưng
What is the difference between JDK và JRE?
What is “the inverse side of the association” in a bidirectional JPA OneToMany/ManyToOne association?
What's the difference between
Repository &
Service annotations in Spring?
JPA JoinColumn vs mappedBy
What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?
Hot Network Questions more hot questions

Question feed
Subscribe lớn RSS
Question feed To subscribe to this RSS feed, copy và paste this URL inkhổng lồ your RSS reader.

Stachồng Overflow
Staông xã Exchange Network
site thiết kế / logo sản phẩm © 2021 Staông xã 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 Stachồng Exchange can store cookies on your device and discđại bại information in accordance with our Cookie Policy.