views:

448

answers:

3

Is there a way to delete an entity without having to fetch it from the datastore first? I am assuming I already have the key or id for the entity. I'm thinking of something like deleteObjectById that would be an analogue to getObjectById on PersistenceManager.

The closest I can think of is using Query.deletePersistentAll() (as seen here) and specifying a query that only relies on the key, but I can't tell if that is going to fetch the entity before deleting it.

thanks

EDIT: I know how to do this using the low level API, as well as in the python API. I was wondering if there was a way to do it within the JDO layer.

A: 

How about using the Low-level API?

I think DataService.delete(Key) will do exactly what you need.

If you use a Long as key, you will ned to use the KeyMaker to create a key first.

Key k = KeyFactory.createKey(Employee.class.getSimpleName(), "[email protected]");
Fedearne
I know the functionality exists in the datastore, I am trying to determine whether I can access it from within the JDO layer.
Peter Recore
+1  A: 

datanucleus answered my question on the google group:

Not really. The issue is that an object can have relations and so, since these can cause cascade of operations, it typically has to be loaded into memory first. The only situation where it wouldn't need to be loaded into memory was if the class of the object to be deleted had no relations.

Going into the low level API and blasting away objects may or may not have an impact on related objects ... perhaps someone responsible for it could comment on that.

Peter Recore
A: 

I'm not sure about JDO, but for Python this is possible with a small hack. You need to "create" a new instance with the same key and then delete it. It will hit the database only once.

instance = Model(key=key_to_delete, required_property='dummy')
instance.delete()
jbochi