CriteriaBuilder throws an exception when trying to build a select based on an embeddedId property if the entity extends MappedSuperclass and the MappedSuperclass contains a generic embedded ID. Issue occurs with the following mappedSuperclass for composite keys (EmbeddedId) and concrete entities implementing this abstract superclass:
@MappedSuperclass
public abstract class AbstractEmbeddedIdEntity<ID extends Serializable> {
@EmbeddedId
private ID id;
public AbstractEmbeddedIdEntity() {
id = newId();
}
public ID getId() {
return id;
}
public void setId(ID id) {
this.id = id;
}
protected abstract ID newId();
}
Now the issue is that if multiple entities exist and I try to access an ID property of an entity with CriteriaBuilder, an exception is thrown (for all except one entity):
java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [firstId] on this ManagedType [unknown]
Seems that the metamodel maps all of my entities to the same MappedSuperclass instance. During application startup, the properties of the `@EmbeddedId` returned by `newId()` are set to the id type attributes of the MappedSupperclass, *overwriting* the id type attributes of the previous entity. So in the end, all entities are mapped to the same MappedSupperclass, but the MappedSupperclass only has the id type attributes of the last entity. I have create a test case here. Probably related to
HHH-10690 Open and
HHH-10428 Open (however these issues only mention issues with getting the correct type, while in my case the query creation fails). |