3.6.4.2. Entities as Identity Fields

OpenJPA allows ManyToOne and OneToOne relations to be identity fields. To identify a relation field as an identity field, simply annotate it with both the @ManyToOne or @OneToOne relation annotation and the @Id identity annotation.

When finding an entity identified by a relation, pass the id of the relation to the EntityManager.find method:

Example 77. Finding an Entity with an Entity Identity Field
public Delivery createDelivery(Order order) {
    Delivery delivery = new Delivery();
    delivery.setId(order);
    delivery.setDelivered(new Date());
    return delivery;
}

public Delivery findDelivery(EntityManager em, Order order) {
    // use the identity of the related instance
    return em.find(Delivery.class, order.getId());
}

When your entity has multiple identity fields, at least one of which is a relation to another entity, you can use an identity class (see Identity Class in the JPA Overview), or an embedded identity object. Identity class fields corresponding to entity identity fields should be of the same type as the related entity’s identity. If an embedded identity object is used, you must annotate the relation field with both the @ManyToOne or @OneToOne relation annotation and the @MapsId annotation.

Example 78. Id Class for Entity Identity Fields
@Entity
public class Order {

    @Id
    private long id;

    ...
}

	/**
	 * LineItem uses a compound primary key. Part of the compound key
	 * LineItemId is relation or reference to Order instance.
	**/
@Entity
@IdClass(LineItemId.class)
public class LineItem {

    @Id
    private int index;

    @Id
    @ManyToOne
    private Order order;

    ...
}

public class LineItemId {

    public int index;
    public long order; // same type as identity of Order i.e Order.id
                       // also the variable name must match the name of the
                       // variable in LineItem that refers to Order.
}

In the example above, if Order had used an identity class OrderId in place of a simple long value, then the LineItemId.order field would have been of type OrderId.

Example 79. Embedded Id for Entity Identity Fields
@Entity
public class Order {

    @Id
    private long id;

    ...
}

	/**
	 * LineItem uses a compound primary key. Part of the compound key
	 * LineItemId is relation or reference to Order instance.
	**/
@Entity
public class LineItem {

    @EmbeddedId LineItemId id;

    @ManyToOne
    @MapsId("orderId")     // The value element of the MapsId annotation
                           // must be used to specify the name of the primary
                           // key attribute to which the relationship
                           // corresponds. If the primary key referenced by
                           // the relationship attribute is of the same Java
                           // type as the dependent's primary key, then the
                           // value element is not specified.
    private Order order;

    ...
}

@Embeddable
public class LineItemId {

    public int index;
    public long orderId;
}

In the example above, the LineItem uses an embedded id to represent its primary key. The primary key attribute corresponding to the relationship in the LineItemId must be of the same type as the primary key of the Order. The MapsId annotation must be applied to the relationship field LineItem.order.