Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 330444 - Misleading exception with incorrect derived id mapping
Summary: Misleading exception with incorrect derived id mapping
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Eclipselink (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows Vista
: P2 minor (vote)
Target Milestone: ---   Edit
Assignee: Nobody - feel free to take it CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-17 07:50 EST by Sabine Heider CLA
Modified: 2022-06-09 10:35 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sabine Heider CLA 2010-11-17 07:50:58 EST
A user tried the following example (taken from the "Pro JPA 2" book, listings 10-14, 10-5 but modified incorrectly):

@Entity
public class Project {
    @EmbeddedId private ProjectId id;

    @MapsId("dept")
    @ManyToOne
    @JoinColumns({
        @JoinColumn(name="DEPT_NUM", referencedColumnName="NUM"),
        @JoinColumn(name="DEPT_CTRY", referencedColumnName="CTRY")})
    private Department department;
    //...
}

@Embeddable
public class ProjectId implements Serializable {
    @Column(name="P_NAME")
    private String name;

    @Embedded
    private DeptId dept;
    //...
}

@MappedSuperclass  // this is the error, must be @Entity
public class Department {

    @EmbeddedId
    private DeptId id;
    
    @OneToMany(mappedBy="department")
    private List<Project> projects;
    //...
}

@Embeddable
public class DeptId {
    @Column(name="NUM")
    private int number;
    @Column(name="CTRY")
    private String country;
    //...
}


The error message thrown was:

[EclipseLink-7298]: The mapping [dept] from the embedded ID class [class imported.ProjectId] is an invalid mapping for this class. An embeddable class that is used with an embedded ID specification (attribute [id] from the source [class imported.Project]) can only contain basic mappings. Either remove the non basic mapping or change the embedded ID specification on the source to be embedded.

This exception is somehow misleading as it does not identify the root cause (the wrong relationship mapping to a non-entity). It is also not quite true: the embeddable may also contain other embeddables. 
Is there maybe any chance to have this improved and throw a more suitable exception?
Comment 1 Sabine Heider CLA 2010-11-18 08:17:28 EST
It seems that I was mistaken with my last remark about nested embeddables to be used as embedded ids because when I tried it out, I ended up with the same [EclipseLink-7298] exception. I think, however, that they should actually work - at least I couldn't find any restriction like that in the JPA 2 spec. It only says that the embeddable must contain neither relationships nor @Id annotations. Or is there another statement in the spec that I missed? 
If you agree with me that nested embeddables should also be usable as embedded ids, I would file a separate bug for that. Please let me know.
Comment 2 Tom Ware CLA 2010-11-18 09:00:37 EST
My initial look at the spec indicates we should support Embeddables as you mention above.  This issue appears to be valid.
Comment 3 Tom Ware CLA 2010-11-18 09:07:26 EST
BTW: The relationship to a MappedSuperclass is incorrect.  To truely verify this bug we would have to verify it occurs when that relationship is mapped correctly.
Comment 4 Sabine Heider CLA 2010-11-21 13:59:19 EST
Hi Tom,
I know that the mapping above is invalid. (The example actually works quite well once you map the class Department as @Entity instead of @MappedSuperclass.) My point here is simply that the error message was so confusing to our application developer that he was unable to find out himself what was wrong. A message like "The target of the many-to-one relationship Project.department is not an entity" or something like that would have been much more helpful. I was just wondering whether it would be possible to improve the error message for that case.

The other issue is actually a separate issue, and I have opened a separate bug for it: Bug 330755. The problem is somehow related though, because it came up when the same user explained to me what he actually wanted to achieve with the erroneous mapping.
Comment 5 Tom Ware CLA 2010-12-01 10:24:52 EST
Setting target and priority.  See the following page for the meanings of these fields:

http://wiki.eclipse.org/EclipseLink/Development/Bugs/Guidelines
Comment 6 Guy Pelletier CLA 2010-12-29 13:57:20 EST
With the fix to bug 330755, the exception now seen on this model (when Department is a tagged as a mapped superclass) is:

Exception Description: [class org.eclipse.persistence.testing.models.jpa.ddlgeneration.Project] uses a non-entity [class org.eclipse.persistence.testing.models.jpa.ddlgeneration.Department] as target entity in the relationship attribute [field department].

That exception was being hidden with the exception from bug 330755.
Comment 7 Eclipse Webmaster CLA 2022-06-09 10:35:08 EDT
The Eclipselink project has moved to Github: https://github.com/eclipse-ee4j/eclipselink