one-to-one bidirectional association

/, JPA/one-to-one bidirectional association

In a relational database in a one-to-one relationship, a row in table X can have no more than one matching row in table Y, and vice versa. X one-to-one relationship is created if both of the related columns are primary keys or have unique constraints.

In Object oriented programming, one instance of entity exactly refers to one instance of another entity in the  relation called one-to-one relation.

what is bidirectional?

  • Bidirectional relationship provides navigational access in both directions, so that you can access the other side entity without explicit queries.

Tables structure in Database :

User.java Mapping

mappedBy :

The mappedBy element defines a bidirectional relationship. This attribute allows you to refer the associated entities from both sides.

Credentials.java Mapping

Cascade :

  • Whenever rows in the parent table manipulated (inserted, updated, deleted) the respective rows of the child table with a matching key column will be manipulated as well. This is called Cascade in Database.
  • JPA translates entity state transitions to database DML statements.

@JoinColumn :

  • @JoinColumn Specifies a column for joining an entity association or element collection. The annotation MARKDOWN_HASHf6fe5d6d260954e3d74370aaeb356271MARKDOWN_HASH indicates that this entity is the owner of the relationship. That is the corresponding table has a column with a foreign key to the referenced table.
To Understand mappedBy, Cascade and @JoinColumn mapping in entity associations see :
Key points to understand associations

Saving entities  :

Console Output :

Testing bidirectional  navigation:

Console Output :


Download Application – JPA-OneToOne-BiDirection.zip (15 KB)

Leave A Comment