Java Reference
In-Depth Information
Notice that the field is decorated with a @ManyToOne annotation. This annotation
marks the many side of the one-to-many relationship between Customer and
Address . Notice that the field is also decorated with the @JoinColumn annotation.
The name attribute of this annotation indicates the column in the database our
entity maps to that defines the foreign key constraint between the ADDRESS and
CUSTOMER tables (in our case, the CUSTOMER_ID column on the ADDRESS table). The
referencedColumnName attribute of @JoinColumn is used to indicate the primary
key column of the table on one side of the relationship (in our case, the CUSTOMER_ID
column in the CUSTOMER table).
In addition to one-to-many and many-to-one relationships, JPA provides annotations
to denote many-to-many and one-to-one relationships. In our schema, we have a
many-to-many relationship between the CUSTOMER_ORDER and ITEM tables; since
an order can have many items, and an item can belong to many orders.
The table to hold orders was named CUSTOMER_ORDER since the word
"order" is a reserved word in SQL.
Let's take a look at the CustomerOrder JPA entity to see how the many-to-many
relationship is defined:
package com.ensode.jpa;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@Table(name = "CUSTOMER_ORDER")
Search WWH ::

Custom Search