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

      150

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:


Share
Improve this answer
Follow
edited Oct 10 "đôi mươi 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 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ì

Performance

The second aspect is related lớn performance.


Share
Improve this answer
Follow
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
2
Add a bình luận |
11
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.


Share
Improve this answer
Follow
answered Jul 26 "13 at 19:54
Constantino CronembergerConstantino Cronemberger
1,7902020 silver badges2828 bronze badges
Add a phản hồi |
9
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:

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 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ồ.


Share
Improve sầu this answer
Follow
answered Mar 19 "11 at 8:03
kvistakvista
4,95211 gold badge2121 silver badges2424 bronze badges
1
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
Submit

Post as a guest


Name
Email Required, but never shown


Post as a guest


Name
E-Mail

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
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 lớn distinguish one side of a bidirectional relationship in orm as an owning side?
3
Conditional/composite JoinColumn In JPA/Hibernate
2
Insert data into lớn 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 khổng lồ another same entity
2
Update generated after insert when bidirectional one khổng lồ many mapping
0
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
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 & StringBuffer
1150
What are the possible values of the Hibernate hbm2ddl.auto configuration & what vì chưng they vì chưng
986
What is the difference between JDK và 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 lớn RSS
Question feed To subscribe to this RSS feed, copy và paste this URL inkhổng lồ your RSS reader.


lang-java
Stachồng Overflow
Products
Company
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.

*