It’s time to continue articles about Hibernate. The last one was dedicated to unidirectional @OneToOne association. So today I will show you how to obtain a bidirectional @OneToOne primary key association. An example from this tutorial is based on the previous article. Let’s get started.

I will work with the same tables which I have created previously. In order to setup a bidirectional one to one association I need to update two POJOs and the way of saving the process. Let’s consider a new version of the Author class:

import javax.persistence.*;

@Entity
@Table(name="authors")
public class Author {

	@Id
	@GeneratedValue
	private Integer id;
	
	private String name;
	
	@OneToOne(mappedBy="author", cascade=CascadeType.ALL)
	private Biography biography;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Biography getBiography() {
		return biography;
	}

	public void setBiography(Biography biography) {
		this.biography = biography;
	}
	
}

The changes are minimal. I have just removed @PrimaryKeyJoinColumn from the biography field. In the bidirectional association two sides of association appear – owning and inverse. For one to one bidirectional relationships, the owning side corresponds to the side that contains the appropriate foreign key. In our case the owning side is the Author class. Let’s go ahead.

Quote from JPA 2 specification:

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

The inverse side in this example is the Biography class. It requires more essential changes comparing with the Author class.

import javax.persistence.*;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="biographies")
public class Biography {
	
	@Id
	@Column(name="author_id")
	@GeneratedValue(generator="gen")
	@GenericGenerator(name="gen", strategy="foreign", 
	parameters=@Parameter(name="property", value="author"))
	private Integer authorId;
	
	private String information;
	
	@OneToOne
	@PrimaryKeyJoinColumn
	private Author author;

	public Author getAuthor() {
		return author;
	}

	public void setAuthor(Author author) {
		this.author = author;
	}

	public Integer getAuthorId() {
		return authorId;
	}

	public void setAuthorId(Integer authorId) {
		this.authorId = authorId;
	}

	public String getInformation() {
		return information;
	}

	public void setInformation(String information) {
		this.information = information;
	}	
}

The first important thing is decoration of the authorId field with additional annotations.

...
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign", 
parameters=@Parameter(name="property", value="author"))
...

In @GeneratedValue I specify the name of generator (“gen”) and in @GenericGenerator I define the strategy for generator.

The second important thing is adding of the author field to the class with an appropriate getter and setter.

...
	@OneToOne
	@PrimaryKeyJoinColumn
	private Author author;
...

In this way we obtain a bidirectional association. Now we have access to Author from Biography and vice versa, because both objects have references to each other.

After that the process of objects saving must be updated:

...
	public static void main(String[] args) {

		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		
		Author author = new Author();
		author.setName(" O. Henry");
		
		Biography biography = new Biography();
		biography.setInformation("William Sydney Porter  better known as O. Henry...");
		
		author.setBiography(biography);
		biography.setAuthor(author);
		
		session.save(author);
		
		session.getTransaction().commit();
		
		session.close();

	}
...

Notice that now I don’t persist the owning side before adding the inverse side to it. But you can see that I set the biography to the author, and at the following string I set the author to the biography. This is the main purpose of bidirectional association.

The result of the code execution is:

Hibernate: insert into authors (name) values (?)
Hibernate: insert into biographies (information, author_id) values (?, ?)

About The Author

Mathematician, programmer, wrestler, last action hero... Java / Scala architect, trainer, entrepreneur, author of this blog

Close