Table of contents
1.
Introduction
2.
Directions in Entity Relationships
2.1.
Unidirectional Relationships
2.2.
Bidirectional Relationships
3.
Rules in a Bidirectional Relationship
4.
Bidirectional vs Unidirectional Association
5.
Frequently Asked Questions
5.1.
Describe the layout of Hibernate Architecture?
5.2.
Is Session thread safe in Hibernate?
5.3.
What is lazy loading in Hibernate Architecture?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Bidirectional Association

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Bidirectional association allows us to obtain data about a dependent object from both sides. In these situations, we have two classes referencing each other.

Consider Employee and Address classes, where Employee has a reference to Address and Address has a reference to Employee. One-to-one or one-to-many relationships for the classes like these in the mapping file in hibernation are known as bidirectional associations.

Directions in Entity Relationships

A relationship's direction might be bidirectional or unidirectional. There is an owning side and an inverse side to every bidirectional interaction. A unidirectional relationship has just one side that owns. The owner of a relationship controls how the Persistence runtime changes the relationship in the Database.

Unidirectional Relationships

Only one entity in a unidirectional relationship has a relationship field or attribute that relates to the other. LineItem, for example, has a connection field that identifies Product, but Product does not have a relationship field or attribute that identifies LineItem. In other words, LineItem is aware of Product, but Product is unaware of which LineItem instances are referring to it.

Bidirectional Relationships

Each entity in a bidirectional relationship has a relationship field or attribute that references the other entity. An entity class's code can access its linked object via the relationship field or property. An entity is said to "know" about its associated object if it has a related field. They have a bidirectional relationship like if Order knows what LineItem instances it has and LineItem knows what Order it belongs to.

Rules in a Bidirectional Relationship

Bidirectional Relationships are subjected to some rules:

1. The inverse side of a bidirectional connection must use the mappedBy element of the OneToOne, OneToMany, or ManyToMany annotation to refer to its owning side. The mappedBy element specifies the property or field in the entity that is the relationship's owner.

2. Many-to-one bidirectional relationships should not define the mappedBy element on the many sides. The relationship's many sides are always the owning side.

3. The owning side corresponds to the side that contains the appropriate foreign key in one-to-one bidirectional relationships.

4. In the case of many-to-many bidirectional relationships, either of the sides can be the owner.

Bidirectional vs Unidirectional Association

A bidirectional association allows navigational access in both directions, allowing you to visit the other side without making explicit queries. It enables you to use cascading options in both directions. There is an owning side and an inverse side to every bidirectional interaction. 

On the other hand, a unidirectional association has just one side that owns. The owner of a relationship controls how the Persistence runtime changes the relationship in the database.

 

You can also check out Has-A Relationship in Java here.

Frequently Asked Questions

Describe the layout of Hibernate Architecture?

Hibernate offers a layered architecture that allows users to work without knowing the underlying APIs. Hibernate uses the database and configuration data to offer the application with persistence services (and persistent objects).

Is Session thread safe in Hibernate?

No, Session object is not thread-safe in Hibernate and intended to be used with-in single thread in the application.

What is lazy loading in Hibernate Architecture?

The Lazy setting determines whether child items are loaded while the Parent Object is loaded. You must achieve this by modifying the parent class's hibernate mapping file. Lazy = True (means not to load child) The sluggish loading of child objects is enabled by default.

Conclusion

This blog discussed the bidirectional association in classes within hibernate and its differences from the unidirectional association.

If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - JDBCJDBC ConnectionGuide to ORMSpring Boot JDBC.

Refer to our Coding Ninjas Studio Guided Path to learn Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and even more! You can also check out the mock test series and participate in the contests hosted by Coding Ninjas Studio! But if you're just starting and want to learn about questions posed by tech giants like Amazon, Microsoft, Uber, and so on. In such a case, for placement preparations, you can also look at the problemsinterview experiences, and interview bundle.

You can also consider our premium courses to offer your career advantage over others!

Please upvote our blogs if you find them useful and exciting!

Happy Coding!

Live masterclass