Should domain objects be free of JPA annotations? #82
Replies: 2 comments 8 replies
-
@patrikfr I believe you have shared some good input on this in the past. Would you like to add your opinion here? |
Beta Was this translation helpful? Give feedback.
-
Infrastructure (the xml-file) depending on domain code is fine. In annotating domain classes with JPA now domain depends on some level of the infrastructure. There's schema information on it. This is a single project but if it was a multi-project, the domain project would need a dependency on persistence API. Ideally, there should be as little dependency on frameworks on domain code, but I think some level of pragmatism can be beneficial. For example, adding transactional annotations, logging or lombok, maybe even annotations to be used by aspects for tracing and metrics. But I think it is too much to add JPA annotations on entities. A similar scenario to me would be if some REST API was returning a domain class, not ideal but manageable, and a requirement came up to change the API you start adding Jackson annotations to the domain class. I would say to just use a DTO, for both cases, let domain entities be free of framework annotations. |
Beta Was this translation helpful? Give feedback.
-
In #73 this question showed up:
which was answered by:
Good question. Prior to this refactoring the Hibernate mapping was also closely tied to the domain objects even though the connection was not in the java-code itself but in a xml-file. So I would argue that we have not changed the design and that your question is about if this is a preferable design or not.
What do you think? Should JPA annotations be separated from domain objects or not?
Beta Was this translation helpful? Give feedback.
All reactions