588 words

Handling deleted references

#1
2016-11-18 14:34

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:

@PostLoad
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

doppelrittberger
doppelrittberger's picture
Joined on 2016-01-13
User Post #15
#2
2016-11-18 17:45

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)
support
support's picture
Joined on 2010-05-03
User Post #2,652
#3
2016-11-21 07:14

Hi,

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.

Thanks

Markus Ritter

doppelrittberger
doppelrittberger's picture
Joined on 2016-01-13
User Post #16
#4
2016-11-21 08:35

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)
support
support's picture
Joined on 2010-05-03
User Post #2,662

Post Reply

Please read carefully the posting instructions - before posting to the ObjectDB website.

  • You may have to disable pop up blocking in order to use the toolbar (e.g. in Chrome).
  • Use ctrl + right click to open the browser context menu in the editing area (e.g. for using a browser spell checker).
  • To insert formatted lines (e.g. Java code, stack trace) - select a style in the toolbar and then insert the text in the new created block.
  • Avoid overflow of published source code examples by breaking long lines.
  • You may mark in paragraph code words (e.g. class names) with the code style (can be applied by ctrl + D).
  • Long stack traces (> 50 lines) and complex source examples (> 100 lines) should be posted as attachments.
Attachments:
Maximum file size: 32 MB
Cancel