Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 367797

Summary: Invalid validation for derived identities with MapsId
Product: [WebTools] Dali JPA Tools Reporter: Nan Li <nan.n.li>
Component: JPAAssignee: Neil Hauge <neil.hauge>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: neil.hauge, paul.fullbright
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Nan Li CLA 2012-01-03 16:34:07 EST
Build Identifier: I-3.4.0-20111227220343

An validation error is given against the subclass Dependent with the case below saying "An ancestor of this class has already defined the primary key. Primary key attributes may not be defined here." but the subclass is just using a MapsId, not really defining a primary key. Based on our understanding of MapsId -- it marks an existing primary key field as being reused for a relationship, the validation error is invalid.

@MappedSuperclass
public abstract class Person {
	@Id
	int empId;
}

public class Dependent extends Person {
	@ManyToOne
	@MapsId
	protected Employee emp;
}

**Please comment if you have different understanding of MapsId. 

Reproducible: Always
Comment 1 Nan Li CLA 2012-01-03 16:35:43 EST
Dependent class given in the bug description should be annotated with @Entity.
Comment 2 Neil Hauge CLA 2012-02-16 12:50:35 EST
I assume this only applies to Generic platforms?  Did we get input from the EclipseLink guys on this?
Comment 3 Nan Li CLA 2012-02-20 14:26:39 EST
yes, the problem only appears with Generic platforms.
Comment 4 Neil Hauge CLA 2012-04-09 17:44:03 EDT
Paul...can you comment on this?
Comment 5 Paul Fullbright CLA 2012-04-10 10:53:20 EDT
I suppose it's a matter of interpretation.

From the spec: "If the dependent entity has a single primary key attribute (i.e, the relationship attribute or an attribute
that corresponds to the relationship attribute) and the primary key of the parent entity is a simple pri-mary key, the primary key of the dependent entity is a simple primary key of the same type as that of the
parent entity (and neither  EmbeddedId nor  IdClass is specified).  In this case, either (1) the rela-tionship attribute is annotated Id, or (2) a separate Id attribute is specified an d the relationship attribute
is annotated MapsId  (and the  value element of the MapsId  annotation is not specified)."

Does the relationship determine the id?  Or does the id determine the relationship?

Also from the spec: "If a many-to-one or one-to-one entity relationship corresponds to a primary key attribute, the entity con-taining this relationship cannot be persisted without the relationship having been assigned an entity
since the identity of the entity containing the relationship is derived from the referenced entity."

It seems to me in this case that the relationship is determining the id, and therefore, whether the dependent class chooses to model this as a @ManyToOne @Id or a @ManyToOne @MapsId with a separate @Id field is just matter of choice, but that the mechanism is the same - the relationship is determined first, then the id.