@JoinColumn(name = "US_STATE_ID",
referencedColumnName = "US_STATE_ID")
private UsState usStateId;
@JoinColumn(name = "CUSTOMER_ID",
referencedColumnName = "CUSTOMER_ID")
private Customer customerId;
@JoinColumn(name = "ADDRESS_TYPE_ID",
referencedColumnName = "ADDRESS_TYPE_ID")
private AddressType addressTypeId;
//constructors, getters, setters, equals(), hashCode() and
//methods deleted for brevity. }
Notice that the Address entity has a customerId field, this field is of type Customer ,
the entity we were just discussing.
We admit that a more appropriate name for this field would have been
customer, the New Entity Classes from Database names the field based
on the column name in the database. This is one small disadvantage of
using the wizard to generate JPA entities. Of course we are free to rename
the field and the corresponding getter and setter methods, additionally;
we would have to change the value of the mappedBy attribute of the @
OneToMany annotation on the other side of the relationship.
Noticed 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. The referencedColumnName attribute of @JoinColumn is use to indicate the
primary key column of the table on the "one" side of the relationship ( CUSTOMER , in
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
many-to-many relationships between the CUSTOMER_ORDER and ITEM tables, since an
order can have many items, and an item can belong to many orders.