We would love to see constructor strategy land into Hibernate to enable Entity dependency injection. We try to follow Domain Driven Design to have a more maintenable application. To execute a command, an application service must retrieve the concerned aggregate from a repository. The aggregate is responsible for mutating the state of its entities. Therefore, the aggregate needs to have a access to the ACL (Access Control List) provider. Today, there is a way to inject the ACL provider (and any service) by creating a JPA listener that would set the provider on each entity load. So, dependency injection via setter is possible. But, when you have this kind of injection, unit testing (without the dependency injection system started) becomes harder. The test writer must know which dependency is mandatory. Constructor injection solves this difficulty => The test writer cannot build the entity without its dependencies. |