title: Onion Architecture using JPA Entity as Domain Entity
body: <p>I've been struggling to choose to work with JPA entities as separated classes than domain entities in a single bounded context. I've faced the following choices</p>
  
  <p>Use separated domain classes for Aggregate roots/Aggregates..etc with domain repositories to wrap Spring JPA repositories and use converters to map JPA entities &lt;> Domain Entities with only required data</p>
  
  <ul>
  <li><p>Lazy loading is about to be given away unless in mappers/converters are handling this inside domain repositories but this is overkill.</p></li>
  <li><p>When saving objects, there might be related Aggregate roots (one to many relationship) which later in complex logic, I had to extremly take care of the state of the Domain entity to pass it to the domain repository and either fill it with all related data or simply map it (another method in the converter) with out relationship data (cascading not applied on JPA persisting)</p></li>
  <li><p>A lot of duplicated code to avoid such situations even for very simple use cases</p></li>
  </ul>
  
  <p>Or Use JPA entities as my domain entities and so far there are multiple examples/opinions of this like</p>
  
  <p><a href="https://github.com/citerus/dddsample-core/tree/Spring_Annotations_Autowire" rel="nofollow noreferrer">https://github.com/citerus/dddsample-core/tree/Spring_Annotations_Autowire</a></p>
  
  <p><a href="http://www.javamagazine.mozaicreader.com/MayJune2018/Twitter#&amp;pageSet=50&amp;page=0" rel="nofollow noreferrer">http://www.javamagazine.mozaicreader.com/MayJune2018/Twitter#&amp;pageSet=50&amp;page=0</a></p>
  
  <p><a href="https://stackoverflow.com/questions/46227697/should-jpa-entities-and-ddd-entities-be-the-same-classes">Should JPA entities and DDD entities be the same classes?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/31400432/ddd-domain-entities-vo-and-jpa">DDD, domain entities/VO and JPA</a></p>
  
  <p><a href="https://stackoverflow.com/questions/49934939/how-to-implement-ddd-using-spring-crud-jpa-repository">How to implement DDD using Spring Crud/Jpa Repository</a></p>
  
  <p>On the other hand, there are opinions like this</p>
  
  <p><a href="https://stackoverflow.com/questions/15837567/is-it-a-good-practice-to-use-jpa-entities-as-domain-models">Is it a good practice to use JPA entities as domain models?</a></p>
  
  <p>My question, on the long run, from experience</p>
  
  <ul>
  <li>What would cost more effort &amp; time ?</li>
  <li>Are both approaches are acceptable as practices ?</li>
  <li>What are the pros and cons of both ?</li>
  </ul>