This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 258689 - [JPA 2.0] support for derived identities
Summary: [JPA 2.0] support for derived identities
Status: RESOLVED FIXED
Alias: None
Product: Dali JPA Tools
Classification: WebTools
Component: General (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: 2.3 M2   Edit
Assignee: Paul Fullbright CLA
QA Contact:
URL:
Whiteboard: JPA2.0
Keywords: plan
Depends on:
Blocks: 258500
  Show dependency tree
 
Reported: 2008-12-12 14:00 EST by Karen Butzke CLA
Modified: 2010-04-06 16:48 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Karen Butzke CLA 2008-12-12 14:00:12 EST
Support for derived identities - see section 2.4.1 of the JPA spec
Here are the examples from the spec.  The key thing that jumps out at me that is different from how we handle things now is the @ManyToOne being labeled as @Id.

The parent entity has a simple primary key:
@Entity
public class Employee {
@Id long empId;
String name;
...
}
Case (a): The dependent entity uses IdClass to represent a composite key:
public class DependentId {
String name; // matches name of @Id attribute
long emp; // matches name of @Id attribute and type of Employee PK
}
@Entity
@IdClass(DependentId.class)
public class Dependent {
@Id String name;
@Id @ManyToOne Employee emp; ...
}
Sample query:
SELECT d
FROM Dependent d
WHERE d.name = 'Joe' AND d.emp.name = 'Sam'
Case(b): The dependent entity uses EmbeddedId to represent a composite key:
@Embeddable
public class DependentId {
String name;
long empPK; // corresponds to PK type of Employee
}
@Entity
public class Dependent {
@EmbeddedId DependentId id;
...
@MappedById("empPK") // maps to empPK attribute of embedded id
@ManyToOne Employee emp;
}
Sample query:
SELECT d
FROM Dependent d
WHERE d.id.name = 'Joe' AND d.emp.name = 'Sam'
Comment 1 Karen Butzke CLA 2008-12-12 14:02:05 EST
241765 - EclipseLink support for derived identities is targeted for 2.0
Comment 2 Karen Butzke CLA 2009-02-19 15:46:15 EST
I've looked into what it might take for us to support derived identities or at least tolerate them (not give invalid validation errors).  One possibility for tolerance is to override the process of building specified mappings in AbstractJavaPersistentAttribute.  So, a specified id mapping that also defaults to 1-1 in EclipseLink would now have a null specified mapping and a default 1-1.  The user would see it as a default 1-1 mapping.  We also would need to change the order of how we read annotations in EclipseLink, making sure to read OneToOne and ManyToOne before Id.  We might need to change the validation for one-to-one and many-to-ones if they are derived identities (check the resource model for the Id annotation).  We could potentially just disable validation in this case.

I think that would cover us for just the tolerance use case.
Comment 3 Neil Hauge CLA 2009-08-27 12:37:56 EDT
This didn't quite make M1.  All of the core refactoring and most of the model work is in place.  Minor UI changes are required to complete this feature.
Comment 4 Paul Fullbright CLA 2009-09-04 16:13:42 EDT
functionally complete and committed to head
Comment 5 Nathan Bryant CLA 2010-04-05 13:16:39 EDT
Not sure this is working properly in 3.2.0M6. I'm seeing the error "The attribute matching the ID class attribute parent does not have the correct type long". Am I missing something?
Comment 6 Nathan Bryant CLA 2010-04-05 13:39:56 EDT
Figured this one out. The problem was that I had:

@Entity
public class Parent {
    @Id Long id;
}

public class ChildPK implements Serializable {
    private long parent;
    private int someOtherParentId;
}

When ChildPK.parent is changed to "Long", it works. Either this is a case of the validations producing the wrong error message for a real problem, or there should be no error at all.
Comment 7 Neil Hauge CLA 2010-04-06 16:48:07 EDT
(In reply to comment #6)
> Figured this one out. The problem was that I had:
> 
> @Entity
> public class Parent {
>     @Id Long id;
> }
> 
> public class ChildPK implements Serializable {
>     private long parent;
>     private int someOtherParentId;
> }
> 
> When ChildPK.parent is changed to "Long", it works. Either this is a case of
> the validations producing the wrong error message for a real problem, or there
> should be no error at all.

Looks like we probably need to relax this validation to account for autoboxing.  Please open a bug for this issue so we can track it separately from this enhancement.  Thanks for bringing this to our attention.  For M6, you could disable this error in our Errors/Warnings preferences as a workaround.