can removing an entity throw an exception if the entity is in use?


Suppose you have an entity @Entity class A { ... }

and other entities with references to A.   e.g.

@Entity class B {
   A a;  // explicit reference to A
   List<A> list; // list of A's
   Set<A> set; // set of A's
   Set untyped; // general collection that may include A's

Objectdb allows me to remove an instance of A, even if a managed B has some reference to that A. When I then find the B instance and try to reference the A, I get an EntityNotFoundException.

Is there a way to prevent the removal of the A, if it is referenced by any other object in the database? (ie. EntityManager.remove() should throw an exception.)






It is the application responsibility to avoid delete operations that may cause dangling (broken) references.

The ObjectDB Doctor reports broken references so it could be very helpful during testing and debugging in identifying such problems. The fix, however, is required at the application level.

ObjectDB Support

Is there a query that can return all Objects (@Entity and/or @Embedded) that include any reference to a specific Entity?


You can try something like:

SELECT DISTINCT b FROM A a, B b WHERE a = b.a OR a MEMBER OF b.list ...

The query will have to be adjusted to your specific model.

It might be a slow query. Maybe indexes on the relevant fields of B could help.

ObjectDB Support

Actually if you are interested in references to a specified A instance the query is simpler:

SELECT DISTINCT b FROM B b WHERE :a = b.a OR :a MEMBER OF b.list ...


ObjectDB Support