Spring-date JPA: modeling graph getting column breaks non-null when removing edges

I have a set of objects that create some kind of graph. This is modeled by a class Entity

with two fields that simulate relationships between objects.

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "fromId")
private Set<EntityRelation> outEdges;

@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "toId")
private Set<EntityRelation> inEdges;

      

All outEdges must belong to the entity when it is saved, inEdges are "deduced" from them. The problem is that after removing the outer edge, I always get an error ERROR: null value in column "fromid" violates not-null constraint

where fromid

is the margin EntityRelation

.

For performance reasons, I do not want to be in the form of direct relations Entity

to Entity

.

To fix this, I used the new Spring-Data JPA method (in the appropriate repository class) to explicitly delete all the objects that the object points to (for example,

@Modifying
@Query(value = "delete from entityrelation where fromid = ?1", nativeQuery = true)
int deleteEntityRelations(String entityId);

      

But that somehow misses the whole point, as I want the JPA to take responsibility.

what is wrong here? I'm really stuck as all the posts I could find show that it should just work with orphan-delete

.

In SQL-Trace, you can see what org.hibernate.SQL - update EntityRelation set fromId=null where fromId=? and id=?

is thrown automatically (which then causes the error).

thanks and welcome Frike

+3


source to share


2 answers


This is a known hibernation issue. In certain scenarios (and you've found one), this breaks constraints on foreign key relationships. There are various options (but I'm afraid you may not like any of them)



  • remove restriction. I know, I know.

  • make the limitation deferred . Not sure if this feature is available in other databases besides Oracle.

  • limit JPA expectations. Seriously, it looks like you expect more from this, then it will give you. I highly recommend reading this article before embarking on any project using any ORM.

+2


source


Note that even when set hbm2ddl.auto

to, UPDATE

it does not remove non-null constraints when the object is nullable

set to FALSE

. I would suggest checking the class history for any entity relationship changes or column mappings for a NULL constraint.



+1


source







All Articles