background image

LineItemKey Class

<< Generated Primary Keys | Compound Primary Keys >>
<< Generated Primary Keys | Compound Primary Keys >>

LineItemKey Class

In order, two entities use compound primary keys: Part and LineItem.
Part
uses the PartKey wrapper class. Part's primary key is a combination of the part number
and the revision number. PartKey encapsulates this primary key.
LineItem
uses the LineItemKey class. LineItem's primary key is a combination of the order
number and the item number. LineItemKey encapsulates this primary key. This is the
LineItemKey
compound primary key wrapper class:
package order.entity;
public final class LineItemKey implements
java.io.Serializable {
private Integer orderId;
private int itemId;
public int hashCode() {
return ((this.getOrderId()==null
?0:this.getOrderId().hashCode())
^ ((int) this.getItemId()));
}
public boolean equals(Object otherOb) {
if (this == otherOb) {
return true;
}
if (!(otherOb instanceof LineItemKey)) {
return false;
}
LineItemKey other = (LineItemKey) otherOb;
return ((this.getOrderId()==null
?other.orderId==null:this.getOrderId().equals
(other.orderId)) && (this.getItemId ==
other.itemId));
}
public String toString() {
return
"" + orderId + "-" + itemId;
}
}
The @IdClass annotation is used to specify the primary key class in the entity class. In
LineItem
, @IdClass is used as follows:
@IdClass(order.entity.LineItemKey.class)
@Entity
...
The order Application
Chapter 26 · Persistence in the EJB Tier
715