Home » Java » Enterprise Java » JPA: Determining the Owning Side of a Relationship

About Kevin Bowersox

JPA: Determining the Owning Side of a Relationship

When using the Java Persistence API (JPA) it is often necessary to create relationships between two entities.  These relationships are defined within the data model (think database) through the use of foreign keys and within our object model (think Java) using annotations to indicate associations. When defining relationships or associations within the object model a common task is identifying the owning side of the relationship.  Identifying the owning entity within a relationship is important because the owning side is most often, if not always, where the @JoinColumn annotation must be specified. To illustrate the concept of the owning side of an entity we will use a data model to support this discussion.
 
 

Data Model

Let’s analyze this simple model, which depicts a relationship between two tables POST and SERIES.  In this relationship, the POST table stores a blog post, which can be part of a series of posts represented by the SERIES table. In the data model, the SERIES_ID foreign key on the POST table associates the POST with its respective SERIES.  This foreign key indicates which entity owns the relationship. Let’s add these entities in the object model and establish a simple unidirectional relationship between them.  First, the series entity:

@Entity
@Table(name="SERIES")
public class Series {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="SERIES_ID")
	private Integer seriesId;

	@Column(name="TITLE")
	private String title;

	//Accessors...
}

And the Post entity:

@Entity
@Table(name="POST")
public class Post {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="POST_ID")
	Integer postId;

	@Column(name="TITLE")
	String title;

	@Column(name="POST_DATE")
	Date postDate;

	@ManyToOne
	@JoinColumn(name="SERIES_ID")
	private Series series;

	//Accessors...
}

In the Post entity the @JoinColumn annotation is specified above the field Series to denote the foreign key to be used to identify a Post’s respective Series.  The @JoinColumn annotation was placed on the Post entity because it is the owning entity in the relationship.  The owning side of the entity was determined by referencing both entities in the data model and identifying the entity containing the foreign key. If the relationship between the Post and Series entities was required to be bidirectional, meaning the Post entities should be accessible from the Series, the inverse side of the relationship (Series) must be annotated with @OneToMany, with a mappedBy element defined.  The mappedBy element should point to the field on the owning side of the relationship (Post) that specifies the @JoinColumn used to associate the entities.

The mapping for establishing a bidirectional relationship is highlighted in the following refactoring of the Series entity:

@Entity
@Table(name="SERIES")
public class Series {

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="SERIES_ID")
	private Integer seriesId;

	@Column(name="TITLE")
	private String title;

	@OneToMany(mappedBy="series")
	private List posts = new ArrayList();

	//Accessors...
}

In summary, when determining the owning entity within a relationship defined within a JPA persistence unit, it is important to consult the data model to find which entities respective table in the data model contains the foreign key.

 

Reference: JPA: Determining the Owning Side of a Relationship from our JCG partner Kevin Bowersox at the ToThought blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


seven − = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.