Trouble understanding how to lookup objects that have a Datastore Identity by their PK (if the PK is not identified in the model)


ebenzacar@...
 

I'm migrating an application that was writing with Kodo4 (JDO 2.2).  Most of the objects/models use implicit datastore-identity with no fields identified in the model as the PK, but over time, developers have designed in systems to use the Object Ids (PK) to retrieve the objects by their ID.  I realize that this goes against the concept of datastore-identity and over time will need to change them.

In the meantime, however, I am having trouble figuring out how to retrieve an object using it's ID (Long) that is in the DB.

@PersistenceCapable
@DatastoreIdentity(strategy= IdGeneratorStrategy.SEQUENCE, sequence="toto" )
@Sequence(name="toto", datastoreSequence = "jdo_sequence", strategy=NONTRANSACTIONAL)
public class Organization {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}



Current code does the following:

public <T> T getObject( String spId, Class<T>persistentClass ) {
Object
id = pm.newObjectIdInstance(persistentClass, spId);
Object obj =
pm.getObjectById(id);
return persistentClass.cast(obj);
}

Unfortunately, with DataNucleus, this is not allowed.  When I try to call `pm.newObjectIdInstance( clazz, Long id )`, an exception is thrown by the ExecutionContext in org.datanucleus.ExecutionContextImpl#newObjectId(java.lang.Class, java.lang.Object) that the key is not a string and the class is not a SingleFieldIdentity.   If I convert the Long to a string, an exception is thrown that it essentially isn't in the right format.

I think I've hacked around the problem a bit by doing the following but it seems hackish - especially considering that I need to use a DN Core class as part of my code and not just leverage JDO methods
DatastoreId datastoreId = new DatastoreIdImplKodo(clazz.getName(), id);
Object oId = pm.newObjectIdInstance(Organization.class, datastoreId.toString()); return clazz.cast(pm.getObjectById(oId));


Is there no way to accomplish this more cleanly?  Can I identify the class/model differently such that it is identified as a SingleFieldIdentity class?

Thanks,

Eric

Join main@datanucleus.groups.io to automatically receive all group messages.