I have an aggregate root that has 2 entities. One of them also references the other. Due to a foreign key constraints between those entities in the database, when Spring Data JDBC deletes and tries to re-insert the data, I get Key (department_id)=(0DJ61WH12EDHH) is not present in table "departments"
.
我有一个具有2个实体的聚合根。其中一个也引用了另一个。由于数据库中这些实体之间的外键约束,当Spring data JDBC删除并尝试重新插入数据时,我得到的键(Department_Id)=(0DJ61WH12EDHH)不在表“Departments”中。
To check my logical structure I am thinking that:
为了检查我的逻辑结构,我认为:
- deleting an
Organization
should delete all Department
s and all Membership
s (not the User
s, though)
- deleting a
Department
should not be possible until it is being referenced by Membership
- deleting a
Membership
should not delete potentially present Department
(and obviously not the `Organization)
Here's the structure:
结构是这样的:
class Organization { // the aggregate root
@Id
private Integer id;
@MappedCollection(idColumn = "organization_id")
private Set<Department> departments = new HashSet<>();
@MappedCollection(idColumn = "organization_id")
private Set<Membership> memberships = new HashSet<>();
}
class Department {
@Id
private Integer id;
}
class Membership {
@Id
private Integer id;
@Column("organization_id")
private AggregateReference<Organization, String> organization;
@Column("user_id")
private AggregateReference<User, String> user;
@Column("department_id")
private AggregateReference<Department, String> department;
}
So a Membership
is always a combination of Organization
and a User
, and only sometimes a Department
is added. When I do a change on the aggregate root, all the underlying entities get deleted, as expected, however the Membership
is inserted before the Department
, hence the Foreign Key constraint becomes broken.
因此,成员资格始终是组织和用户的组合,只是有时会添加一个部门。当我对聚合根进行更改时,所有底层实体都会被删除,但成员资格被插入到部门之前,因此外键约束被打破。
If I dropped the constraint things work ok, but I obviously don't want to do that. Is there any workaround?
如果我放弃了约束,事情就能正常运行,但我显然不想那样做。有什么解决办法吗?
I tried to drop the constraint, to check if it would work out and it does.
我试着放弃约束,检查它是否会工作,它确实如此。
I also tried having the reference between the Membership
and Department
as an Entity
reference, but that didn't work out, and I guess it goes against DDD.
我还尝试将Membership和Department之间的引用作为实体引用,但没有成功,我想这与DDD相悖。
更多回答
优秀答案推荐
Let's start with some rules about DDD and Spring Data JDBC:
让我们从关于DDD和Spring data JDBC的一些规则开始:
AggregateReference
is intended for references between aggregates.
- entities within an aggregate must not get referenced from outside the aggregate.
That said, I'd think Organization
, Department
, and Membership
should all be separate aggregates.
也就是说,我认为组织、部门和成员都应该是单独的集合。
更多回答
But a Department
only makes sense if there's an Organization
. If I deleted an Organization
, I'd like all ` Departments deleted (actually all "attached"
Membership`'s too). Does DDD handle a situatuon where where an aggregate only exist in the context of another aggregate?
但只有在有组织的情况下,部门才有意义。如果我删除了一个组织,我希望删除所有`部门‘(实际上也包括所有’附加的‘S成员)。如果一个聚合只存在于另一个聚合的上下文中,那么DDD是否处理这种情况?
我是一名优秀的程序员,十分优秀!