I have defined an unowned relationship using JDO 3.0 syntax:
@Persistent
@Unowned
private Set<Role> roles;
I have several predefined roles which should be assigned to users. I use following code for adding/removing roles to users:
roles.add(roleEntity);
roles.remove(roleEntity);
The problem is that removing like this also removes the original entity from the datastore but I just want to remove the reference. I know I could store only Keys inside the parent entity which would solve the problem, but is there a better solution? Using this "natural" syntax?
I guess this question deserves a real final answer.
The described behavior was caused by a bug in JDO and it should be fixed by now. See http://code.google.com/p/datanucleus-appengine/issues/detail?id=290 for more details. Also notice, that owned
relationships are always dependent
, no matter what the documentation says.
Unfortunately, it was fixed after I had moved back to JDO 2.0. In JDO 2.0, I implement unowned
relationships using collections of keys
or using foreign keys
. And actually it's not a bad solution so I don't plan to try JDO 3.0 again. For a collection of keys I use batch fetch
to get child objects, in case of foreign keys, I use simple queries with where
statement. Sometimes it's better to use the first solution, sometimes the second one. It's all covered by a clean API anyway, so the final code is simple and "natural". (I wish this approach was described directly in the documentation so we don't have to learn it the hard way.)
The important thing is that in both cases I can destroy the relationship without deleting original entities. It's a true unowned
relationship.