1

Let's assume we have three aggregates:

  • Employee
  • Evaluation
  • Contract

Both Evaluation and Contract are referencing Employee (using an AggregateReference or the ID itself).

The problem is that both Evaluation and Contract very regularly need specific properties of the Employee for their business-concerns for example the name.

That would mean, that in most cases when loading Evaluations or Contracts i would also need to load the referenced Employee (at least parts of it).

This is very costly, especially when iterating over collections of Evaluations and Contracts.

Another possibility would be to design "EvaluationEmployee" and "ContractEmployee" as sub-entities under their respective aggregate-root. This would solve the problem, because they are now part of the aggregate and spring-data-jdbc would load them together with the aggregate-root.

But now we have another problem: Employees don't have back references to Evaluation and Contract (actually it's the other way round). And there are a few indicators for Employee rather being an aggregate than an Entity: for example Employees are never deleted, when i delete Evaluations or Contracts.

Is there better way to model this?

  • I just found this question: https://stackoverflow.com/questions/63953707/does-spring-data-jdbc-enables-join-foreign-key-rather-than-ids/64025347#64025347 I think the third answer could be the right one for my scenario. I will try it. – Christian Nockemann Sep 30 '22 at 10:13
  • Could you give an example where `Evaluation` and `Contract` would often need the name of the `Employee` for their business rules when processing **commands**? If you mean for queries then that's your problem! – plalx Oct 06 '22 at 00:34
  • Is your problem solved? I so then please either close it or provide an answer. If not please clarify how the situation differs from the question you referenced in the comment. – Jens Schauder Oct 06 '22 at 08:39

1 Answers1

0

I solved my problem by creating two new Entities: "EvaluationEmployee" and "ContractEmployee". Because Spring Data Jdbc needs back-references to work properly, i created a View for each of those Entities with an added back-reference to Evaluation and Contract respectively. This works because i don't need to write the Employees (i only read them).