Fast Object Database for Java - with JPA/JDO support

Handling deleted references


Hi support team,

I have a problem when using DELETE query to delete Entity instances.
We use the following hierarchy:

class User {
    Profile profile

class Profile {

Whenever the profile of a user is set to null we create a new (default) one to achieve that each user has a profile. To reset a profile I run a DELETE query on the old profile , hoping that this would lead to the field "profile" set to "null" but instead I get an object which has the same ID, all default initial values and no version.
To simplify the use case I added a @PostLoad method to the User class, which simply checks if the profile instance is such a "intermediate/deleted" instance.

Is there a way to safely detect such a deleted instance? Or can I disable the creation of these "intermediate" instances? This would simplify my use case since all I have to do is to check if the reference is null and create a new profile instance:

private void checkProfileAvailable() {
    if (profile == null) { // or whatever check to identify previously deleted Entity instances
        profile = ....; // create new Profile instance

Is this possible and how?

Thanks for your help
Markus Ritter


Deleting a Profile entity object and setting the profile field in User (to null or any other value) should be done in the same transaction, to avoid a reference from User to a deleted Profile.

If every Profile instance is owned by exactly one User instance you may consider using orphan removal.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)


yes I know that the application should handle this. But we have several use cases where we cannot check for further references (e.g. if the same Profile instance is used twice in 2 different User instances and we delete one User the Profile is also deleted). Since we have a REST based API one could also simply delete an existing instances which may be references anywhere in the instance tree. Checking for references each time would be too time consuming and complex.

So my question is: is there a reliable way to detect broken references? This would make life so much easier since one can simply add @PostLoad method and recheck all instances if they are broken or not and fix them (e.g. as you suggested set to null). In my opinion this would add valuable function to OBjectDB, because these broken reference objects are sometimes wanted but not in every case useful.


Markus Ritter


A hollow Profile object is created for every User object when it is loaded. That object is expected to be filled with real Profile data from the database (e.g. when accessing the Profile), but if the Profile instance does not exist in the database the object in memory stays hollow. This behaviour cannot be changed.

ObjectDB does not support broken references. You can maintain two unidirectional relationships, i.e. add a list of all the User instances that use a Profile as a field in Profile and then update the users when a Profile is deleted.

Another option to consider, is to have a unidrectional relationship in the other direction, i.e. from Profile to User. This will allow clean deletion of a Profile instance. However, making the many side of the the relationship an owner is not conventional and may not work as expected. You may also have the same problem in the other direction (when a User is deleted), so maybe maintaining two unidirectional relationships and avoiding broken references is the right solution.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply