title: Annotations in domain objects with JPA violates Database is a detail
body: <p>What do you think about have persistence models and domain models separated? I've read that you don't have to mix persistence concerns with your business concerns (DDD, Clean Architecture,MartinFowler, Eric Evans and a lot and a lot of much more). Even so, I still see in all projects domain models annotated directly with ORM annotations like that, being the domain model coupled to the persistence mechanism reaching an anemic model and violating other principles.</p>
  <pre><code>//PersistenceJPA entity
  @Entity
  @Table(name=&quot;training_cycle&quot;)
  class TrainingCycle {
      @Id
      private Long id;
      private String name;
  
      @Column(name=&quot;discipline_id&quot;)
      @JoinColumn(name=&quot;discipline_id&quot;)
      private Long disciplineId; //Referencing by Id because Discipline is another Aggregate ROOT
  
      //EmptyConstructor, getters and setters avoided
  }
  
  //PersistenceJPA entity
  @Entity
  @Table(name=&quot;training_cycle&quot;)
  class Discipline {
      @Id
      private Long disciplineId;
      private String name;
      //EmptyConstructor, getters and setters avoided
  } 
  </code></pre>
  <p>So if you want to follow clean principles you need to split Domain and Persistence Model(as shown below)to have Domain model with business behavior(this avoid anemic model and follows the SRP) and because of that you need to map the domain model to the persistence model (with typical methods like <em>mapToEntity(DomainModel DM)</em> and <em>mapToDomain(PersistenceModel PM)</em> maybe in a mapper/tranformer maybe in the repository class) when you want to interact with Datastore and viceversa when you want to retrieve data from database.</p>
  <pre><code>class Discipline {
      private DisciplineId disciplineId;
      private String name;
  
      public Discipline(DisciplineId disciplineId, String name) {
          this.disciplineId = disciplineId;
          this.name = name
      }
  }
  
  public class TrainingCycle{
      private TrainingCycleId trainingCycleId;
      private String name;
      private DisciplineId disciplineId;
  
      public TrainingCycle(TrainingCyleId trainingCycleId, String name, DisciplineId disciplineId) {
          this.trainingCycleId = trainingCycleId;
          this.name = name;
          assignDiscipline(disciplineId);
      }
  
      public void assignDiscipline(DisciplineId aDisicplineId) {
          if(aDisicplineId == null) {
              throw new IllegalArgumenException(&quot;Discipline cannot be null&quot;)
          }
          this.disciplineId = aDisicplineId;
      }
  }
  
  
  @Entity
  @Table(name=&quot;training_cycle&quot;)
  class TrainingCycleJpa {
      @Id
      private Long id;
      private String name;
  
      @Column(name=&quot;discipline_id&quot;)
      @JoinColumn(name=&quot;discipline_id&quot;)
      private Long disciplineId; //Referencing by Id because Discipline is another Aggregate ROOT
  
      //EmptyConstructor, getters and setters avoided
  }
  
  
  @Entity
  @Table(name=&quot;training_cycle&quot;)
  class DisciplineJpa {
  
      @Id
      private Long disciplineId;
      private String name;
     //EmptyConstructor, getters and setters avoided
  }
  
  class TrainingCyleJpaRepository implements TrainigCycleRepository {
  
      public void create(TrainingCycle trainingCycle) {
          entityManager.persist(this.mapToEntity(trainingCycle)
      }
  
      public TrainingCycle create(TrainingCycleId trainingCycleId) {
          return this.mapToDomain(entityManager.find(TrainingCycleId));
      }
  }
  </code></pre>
  <p>So the discussion/question is Split or not Split Persistence Model from Domain Model? When to split or when not? In the most projects, not to say in all the ones I've seen as an example, I've seen they couple annotations of persistence model in Domain Model when &quot;gurus are allways hawking&quot; DataStore is a detail.</p>
  <p>Thanks a lot.</p>