Ports and Adapters Architecture in Java EE Applications
I want to implement Java EE 7 applications based on the hexagonal architecture (a.k.a. ports and adapters) or onion architecture or clean architecture. Now, the problem is that where I should place my entities. If I place my entities in the application circle then my application will depend on a particular persistence infrastructure, moreover, any kinds of technical infrastructures should not be exposed in the application layer. Thus, the entities should be placed into the persistence adapters, but if so, then when I retrieve entities from the persistence adapter then the dependency rule will be broken, since the rule says that source code dependency can only point inwards, i.e. nothing in an inner circle can know anything at all about something in an outer circle. An alternative could be that I can use DTOs in the application layer and save and retrieve the data through the ports (APIs) that will be defined in the application hexagonal and implemented by the persistence adapters. In this case the duplication of code would be increased definitely, which is a bad design indeed. What should I do in this context?