Should EntityId be unique in the aggregate or globally?

@Aggregate
public class OrderAggregate {

    @AggregateIdentifier
    private String orderId;
    private boolean orderConfirmed;

    @AggregateMember
    private List<OrderItem> orderItems = new ArrayList<>();
}

public class OrderItem {
    @EntityId
    private String productId;

    private int quantity;

    //other fields
}

As shown above, an order may contain different products. I wonder that should EntityId be unique in the aggregate or globally?
I expect that an OrderItem with the same productId but different quantity can exist in different order.

Hi Proust,

The entity id you use for the aggregate members can indeed be reused in different aggregate instances.
To access the exact aggregate member a combination of the aggregate identifier (required to be unique) and the entity id is required.
Thus, using the same entity id but a different aggregate id generates another path to access the member.

So, to answer this:

As shown above, an order may contain different products. I wonder that should EntityId be unique in the aggregate or globally?

The entity id should be unique within the Aggregate instance.

However, this is from the command model’s perspective though.
Depending on what kind of Query Models you are creating and how (if at all) the entity ids are going to be used to retrieve them might introduce different constraints.
As stated to, this is completely defined by what your query models look like.

Hope this helps.

Cheers,
Steven