I have a set of entities that that build some sort of graph. This is modelled by a class Entity
with two fields modelling the relationships between entities.
我有一组构建某种图形的实体。这由一个实体类建模,其中两个字段模拟实体之间的关系。
@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 are supposed to belong to the entity when it is saved, the inEdges are "inferred" from these. Problem is then after removing an out-edge I always get an error ERROR: null value in column "fromid" violates not-null constraint
where fromid
is a field of EntityRelation
.
所有outEdges在保存时都应该属于实体,inEdges是从这些中“推断”的。问题是在删除外边缘之后我总是得到错误ERROR:列“fromid”中的空值违反非空约束,其中fromid是EntityRelation的字段。
For performance reasons, I don't want to have direct relations form Entity
to Entity
.
出于性能原因,我不希望将实体与实体建立直接关系。
To fix this, I used a new Spring-Data JPA method (in the corresponding Repository class) to explicitly remove everything an entity points to (like
为了解决这个问题,我使用了一个新的Spring-Data JPA方法(在相应的Repository类中)来显式删除实体指向的所有内容(如
@Modifying
@Query(value = "delete from entityrelation where fromid = ?1", nativeQuery = true)
int deleteEntityRelations(String entityId);
But this somehow misses the whole point, since I want JPA to take responsibility of that.
但这在某种程度上错过了重点,因为我希望JPA对此承担责任。
what is wrong here? I really got stuck, since all posts I could find suggest that it should simply work with orphan-delete
.
这有什么不对?我真的卡住了,因为我能找到的所有帖子都表明它应该只使用orphan-delete。
In the SQL-Trace you can see that an org.hibernate.SQL - update EntityRelation set fromId=null where fromId=? and id=?
is issued automatically (which then triggers the error).
在SQL-Trace中你可以看到一个org.hibernate.SQL - 更新EntityRelation设置fromId = null其中fromId =?和id =?自动发出(然后触发错误)。
thanks and regards fricke
谢谢,并尊重fricke
2 个解决方案
#1
This is a known issue of hibernate. In certain scenarios (and you found one of them) it violates constraints on foreign key relations. There are various options (but I'm afraid you might not like any of them)
这是一个已知的hibernate问题。在某些情况下(你发现其中一个)它违反了对外键关系的约束。有各种选择(但我担心你可能不喜欢它们中的任何一个)
-
remove the constraint. I know, I know ..
删除约束。我知道我知道 ..
-
make the constraint deferred. Not sure if this feature is available in other databases but Oracle.
使约束延迟。不确定此功能是否在Oracle以外的其他数据库中可用。
-
limit the expectations to JPA. Seriously, it looks like you expecting more from it then it will give you. I highly recommend reading this article before proceeding with any project using any kind of ORM.
限制对JPA的期望。说真的,看起来你期待更多,然后它会给你。我强烈建议您在继续使用任何类型的ORM的任何项目之前阅读本文。
#2
Please note that even though setting hbm2ddl.auto
to UPDATE
, it doesn't remove the not-null type constrains when the nullable
in entity is set to FALSE
. I would suggest that check the history of the class for any changes to entity relationship or column mapping for nullable constraint.
请注意,即使将hbm2ddl.auto设置为UPDATE,当实体中的nullable设置为FALSE时,它也不会删除非null类型约束。我建议检查类的历史记录,以查看实体关系的任何更改或可为空约束的列映射。
#1
This is a known issue of hibernate. In certain scenarios (and you found one of them) it violates constraints on foreign key relations. There are various options (but I'm afraid you might not like any of them)
这是一个已知的hibernate问题。在某些情况下(你发现其中一个)它违反了对外键关系的约束。有各种选择(但我担心你可能不喜欢它们中的任何一个)
-
remove the constraint. I know, I know ..
删除约束。我知道我知道 ..
-
make the constraint deferred. Not sure if this feature is available in other databases but Oracle.
使约束延迟。不确定此功能是否在Oracle以外的其他数据库中可用。
-
limit the expectations to JPA. Seriously, it looks like you expecting more from it then it will give you. I highly recommend reading this article before proceeding with any project using any kind of ORM.
限制对JPA的期望。说真的,看起来你期待更多,然后它会给你。我强烈建议您在继续使用任何类型的ORM的任何项目之前阅读本文。
#2
Please note that even though setting hbm2ddl.auto
to UPDATE
, it doesn't remove the not-null type constrains when the nullable
in entity is set to FALSE
. I would suggest that check the history of the class for any changes to entity relationship or column mapping for nullable constraint.
请注意,即使将hbm2ddl.auto设置为UPDATE,当实体中的nullable设置为FALSE时,它也不会删除非null类型约束。我建议检查类的历史记录,以查看实体关系的任何更改或可为空约束的列映射。