datanucleus-core icon indicating copy to clipboard operation
datanucleus-core copied to clipboard

Consider revising enhancement contract to allow derived identity without use of org.datanucleus.identity.XXXId classes

Open andyjefferson opened this issue 8 years ago • 0 comments

If we have

@Entity
@IdClass(DependentId.class)
public class Dependent
{
    @Id String name;

    @Id
    @ManyToOne 
    Employee employee;
}

@Entity
public class Employee
{
    @Id 
    long id;
}

then we ideally want to define DependentId as

public class DependentId
{
    String name; // matches name of @Id attribute
    long employee; // matches name of @Id attribute and type of Employee PK
   ...
}

but currently we require that "employee" is of type org.datanucleus.identity.LongId dnCopyKeyFieldsToObjectId would need changing to do

Field field = o.getClass().getDeclaredField("name");
field.setAccessible(true);
field.set(o, this.name);

instead of

o.employee = ((LongId)this.employee.dnGetObjectId());

dnCopyKeyFieldsFromObjectId would need changing to do

Object id = new LongId(Employee.class, o.employee);
fc.storeObjectField(0, dnGetExecutionContext().findObject(id, false));

instead of

fc.storeObjectField(0, dnGetExecutionContext().findObject(o.employee, false));

One downside of this possible change is that the current o.employee stored as LongId also contains the class that is being represented, whereas using just a long loses that

andyjefferson avatar Nov 20 '16 07:11 andyjefferson