Trouble understanding how to lookup objects that have a Datastore Identity by their PK (if the PK is not identified in the model)
ebenzacar@...
You could do something like
Eric |
|
Use of implementation-specific classes is always a bad idea IMHO. Imagine hardcoding Kodo-specific stuff and then finding that you need to change provider some time later ...
In the same way, reliance on things outside the JDO spec can be problematic. You could do something like MyClass myObj = pm.getObjectById(MyClass.class, theIdValue);This would likely work whether you are using (single-field) application identity OR datastore identity. And you don't need to cast anything ... I did add an enhancement to datanucleus-core for datastore-id newObjectId, so whether it needs that only you can find out |
|
ebenzacar@...
You mean it obeys the JDO API. Since the "id" for a datastore-identity case as input to pm.newObjectIdInstance has to be the toString() form of the datastore id class then that is to be expected (that you would get back from calling pm.getObjectId()). I understand. So is my approach the "correct" way of retrieving an object from the datastore (rdbms) by it's id (the sequence id that was generated)? Or is there a better approach to accomplish this? Should I be using the DN DatastoreId implementation class directly as in my snippet above? I'm a bit surprised that Kodo allowed me to do this by Long ID only if that is part of the JDO spec. Eric |
|
You mean it obeys the JDO API. Since the "id" for a datastore-identity case as input to pm.newObjectIdInstance has to be the toString() form of the datastore id class then that is to be expected (that you would get back from calling pm.getObjectId()).
The JDO API is managed by the Apache JDO group, which is where you should address any improvement requests. |
|
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. @PersistenceCapable
public <T> T getObject( String spId, Class<T>persistentClass ) { 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);
Eric |
|