ObjectDB JDO - foreign key constraints

#1

Hi,

I am new to both JDO and ObjectDB, and am struggeling to figure out how to define foreign key relationships.

For instance, I have create two classes:

@Entity public class Person  implements Serializable{
    @PrimaryKey private String name;
    private int age;

...}

@Entity public class Company implements Serializable{
    @PrimaryKey private String name;
    private Person employee;
...}

When I save a Company object that points to a Person object (using pm.makePersistent()) I get one entry for the Company and one entry for the Person in the db - as expected.

When I try to delete my Person object (pm.deletePersistent()), I am allowed to do so. Since the person is referred to from the company, I would have preferred to get an exception instead. How can I control this behaviour?

Also, how can I control cascading updates and deletes in JDO? There seems to exist a @ForeignKey annotation in JDO, but it doesn't seem to affect the behaviour of my classes. Also, this annotation does not seem to be present in the online ObjectDB JDO annotation list, so this may not be supported by ObjectDB?

 

Any help appreciated!

#2

ObjectDB doesn't enforce foreign key constraints, and therefore, it is the application responsibility to avoid broken references (e.g. caused by deleting an object but keeping a reference to it).

Regarding cascading operations, even if you use JDO you can still use JPA annotations to set cascading operations. Alternatively you may be able to listen to JDO lifecycle events and then execute cascading operations by using the relevant code.

 

ObjectDB Support

Reply