The mapping [itemId] from the embedded ID class [class example.ItemInventoryCompositeKey] is an invalid mapping for this class


It seems the problem still exist with the latest Cuba version. I tried to use composite key following the example mentioned in the start of this thread, I am getting the error:

> Caused by: Exception [EclipseLink-7298] (Eclipse Persistence Services - org.eclipse.persistence.exceptions.ValidationException
> Exception Description: The mapping [itemId] from the embedded ID class [class example.ItemInventoryCompositeKey] is an invalid mapping for this class. An embeddable class that is used with an embedded ID specification (attribute [itemInventoryCompositeKey] from the source [class example.ItemInventory]) can only contain basic mappings. Either remove the non basic mapping or change the embedded ID specification on the source to be embedded.
> 	at org.eclipse.persistence.exceptions.ValidationException.invalidMappingForEmbeddedId(
> 	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.EmbeddedIdAccessor.addIdFieldsFromAccessors(
> 	at org.eclipse.persistence.internal.jpa.metadata.accessors.mappings.EmbeddedIdAccessor.process(
> 	at org.eclipse.persistence.internal.jpa.metadata.MetadataDescriptor.processMappingAccessors(
> 	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.processMappingAccessors(
> 	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processMappingAccessors(
> 	at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.process(
> 	at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage2(
> 	at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(
> 	at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(
> 	at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(
> 	... 6 more

Any idea how to solve this problem?

Unfortunately, there is no simple way to use association attributes in composite PK, as EclipseLink does not support it.

But you can work around the problem as follows:

Let’s suppose Entity_1 has a composite PK.
The PK is presented by KeyEntity (EmbeddableEntity). It was created by CUBA Studio and has a link to Entity_2.

  1. In the “KeyEntity” modify the link field: make it of basic type instead of @ManyToOne.
public class KeyEntity extends EmbeddableEntity {
    @Column(name = "ENTITY2LINK_ID")
    protected Integer entity2link; //Entity_2 has Integer Id
  1. Then in the Entity_1 create the property entity_2 mark it with @Transient and @MetaProperty, and define its get/set methods similar to following.
protected KeyEntity id;

protected Entity_2 entity_2;

 public Entity_2 getEntity_2() {
        if (entity_2 == null && id.getEntity2link() != null) {
            DataManager dm = AppBeans.get(DataManager.class);
            entity_2 = dm.load(LoadContext
        return entity_2;

    public void setEntity_2(Entity_2 entity_2) {;
        this.entity_2 = entity_2;

This should work.


1 Like


Thanks for the suggestion.

With a composite key of 4 different objects that are not primitives (i.e. integer), the workaround starts to be quite complex (and ugly - please don’t take this personally).

I am trying to find a workaround from the data modelling perspective. If there are any ideas, please share.