Association Mappings: Unidirectional Many-to-One Association
Association Mappings: Unidirectional Many-to-One Association
Association Mappings
Association mappings are one of the key features of JPA and
Hibernate. They model the relationship between two database tables
as attributes in your domain model. That allows you to easily navigate
the associations in your domain model and JPQL or Criteria queries.
JPA and Hibernate support the same associations as you know from
your relational database model. You can use:
one-to-one associations,
many-to-one associations and
many-to-many associations.
You can map each of them as a uni- or bidirectional association. That
means you can either model them as an attribute on only one of the
associated entities or on both. That has no impact on your database
mapping, but it defines in which direction you can use the
relationship in your domain model and JPQL or Criteria queries
Many-to-One Associations
An order consists of multiple items, but each item belongs to only
one order. That is a typical example for a many-to-one association. If
you want to model this in your database model, you need to store the
primary key of the Order record as a foreign key in the OrderItem
table.
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
@Entity
public class OrderItem {
@ManyToOne
@JoinColumn(name = “fk_order”)
private Order order;
…
}
@Entity
public class Order {
@OneToMany
@JoinColumn(name = “fk_order”)
private List items = new ArrayList();
…
}
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
Bidirectional Many-to-One Associations
The bidirectional Many-to-One association mapping is the most
common way to model this relationship with JPA and Hibernate. It
uses an attribute on the Order and the OrderItem entity. This allows
you to navigate the association in both directions in your domain
model and your JPQL queries.
@Entity
public class OrderItem {
@ManyToOne
@JoinColumn(name = “fk_order”)
private Order order;
…
}
The definition of the referencing part is a lot simpler. You just need to
reference the owning association mapping. You can do that by
providing the name of the association-mapping attribute to the
mappedBy attribute of the @OneToMany annotation. In this example,
that’s the order attribute of the OrderItem entity.
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
@Entity
public class Order {
@OneToMany(mappedBy = “order”)
private List items = new ArrayList();
…
}
@Entity
public class Order {
…
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
Many-to-Many Associations
Many-to-Many relationships are another often used association type.
On the database level, it requires an additional association table
which contains the primary key pairs of the associated entities. But
as you will see, you don’t need to map this table to an entity.
@Entity
public class Store {
@ManyToMany
@JoinTable(name = “store_product”,
joinColumns = {@JoinColumn(name = “fk_store”)},
inverseJoinColumns =
{@JoinColumn(name = “fk_product”)})
private List<Product> products =
new ArrayList<Product>();
…
}
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
You can customize the join table with the @JoinTable annotation and its
attributes joinColumns and inverseJoinColumns. The joinColumns
attribute defines the foreign key columns for the entity on which you
define the association mapping. The inverseJoinColumns attribute
specifies the foreign key columns of the associated entity.
@Entity
public class Store {
@ManyToMany
@JoinTable(name = “store_product”,
joinColumns = {@JoinColumn(name = “fk_store”)},
inverseJoinColumns =
{@JoinColumn(name = “fk_product”)})
private List<Product> products =
new ArrayList<Product>();
…
}
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
The mapping for the referencing side of the relationship is a lot
easier. You just need to reference the attribute that owns the
association.
@Entity
public class Product{
@ManyToMany(mappedBy=”products”)
private List<Store> stores = new ArrayList<Store>();
…
}
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
One-to-One Associations
An example for a one-to-one association could be a Customer and
the ShippingAddress. Each Customer has exactly one
ShippingAddress and each ShippingAddress belongs to one
Customer. On the database level, this mapped by a foreign key
column either on the ShippingAddress or the Customer table.
@Entity
public class Customer{
@OneToOne
@JoinColumn(name = “fk_shippingaddress”)
private ShippingAddress shippingAddress;
…
}
www.thoughts-on-java.org
Association Mappings with JPA and Hibernate
The definition of the owning side of the mapping is identical to the
unidirectional mapping.
@Entity
public class Customer{
@OneToOne
@JoinColumn(name = “fk_shippingaddress”)
private ShippingAddress shippingAddress;
…
}
The referencing side of the association just links to the attribute that
owns the relationship.
@Entity
public class ShippingAddress{
@OneToOne(mappedBy = “shippingAddress”)
private Customer customer;
…
}
www.thoughts-on-java.org