-
I want to have an Aggregate with a parent/child relation to self.
Saving the (empty) root object (with JPA) works. But when i save a child of that root, JPA tries to create the root again which leads to an error.
So how do i express recursive links? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Sorry for not seeing this earlier: the reason for the effect you see is that That said, the entity referred to is also an If you really want to get this going as is, you should be able to annotate the |
Beta Was this translation helpful? Give feedback.
Sorry for not seeing this earlier: the reason for the effect you see is that
Location
– by definition – is anEntity
. Entities are "owned" by the aggregates they're contained in, i.e. no two aggregates must refer to the same entity as state changes issued by one aggregate might affect the consistency of the other. Thus, a@OneToOne
mapping is the only reasonable way to map singular properties that are entities.That said, the entity referred to is also an
AggregateRoot
. Following the pure theory, references to an aggregate either should be identifier references or modeled as anAssociation
. That takes the relationship out of the loop for Hibernate's automatic relationship management, as i…