Forking an Entity
Forking a single entity
Given a forkable Ninja, the simplest possible fork is:
Ninja next = original.fork(ForkMode.ToNext);
Under the hood, Forkable.fork(ForkMode) calls fork() to obtain the Forker, then dispatches to toCopy(), toNext(), or toPrevious() based on the mode.
What ends up on the result
The new entity carries:
-
a fresh UUID (assuming your
ForkercallsUUID.randomUUID()); -
forkedFrompopulated byoriginal.toForkSources(mode)— see Lineage; -
references to other forkable entities replaced with fork-ref shells (UUID + lineage, no payload), via
ForkRefAware.ofNullable(…); -
plain data fields copied directly.
Handling references
When a forked entity points at another forkable entity, you have two choices:
-
Inline shell — wrap the reference in
ForkRefAware.ofNullable(other, mode). The parent now has a stable shell that the persistence layer can save without recursively materialising the original. -
Resolved id — call
forkContext.resolveIdentifier(other)for the newSourceIdentifierof the (planned) forked counterpart, and store that. Use this when the referenced entity is also being forked in the same operation: both ends will resolve to the same generated UUID.