0

Is it a good idea to add the mapping code for Entity-Domain conversion directly to the Hibernate Entity?

@Entity
public MyEntity {
    ...

    MyDomain toDomain {...}

    MyEntity toEntity(MyDomain myDomain) { ... }
}

If it isn't a good idea, why and where should we put the mapping code then?

Tom Feiner
  • 164
  • 12

1 Answers1

3

The answer is YES and NO and depends on the conversion.

YES - you can put those methods in the entity class if the conversion from one to other and vice versa is straightforward and there is no complex logic involved. Totally unrelated to this question but on similar context, kotlin has extension functions for exactly this kind of scenarios - extension function for Kotlin data class

NO - If the conversion requires some complex logic, it is better to have it outside the entity so that it is easier to test the conversion in isolation.

Having said that, there is no right or wrong answer to your question. You can choose based on your scenario.

Rishikesh Dhokare
  • 3,559
  • 23
  • 34