Is it a good practice to use JPA entities as domain models?

我的未来我决定 提交于 2020-05-25 04:15:45

问题


or to create a domain layer that is consisted of domain models and talk to JPA entities for database accessing? What are the pros and cons for both approaches ? Thanks!


回答1:


This does really depend on how you code your domain.

In general (in Java) I prefer create a separate set of JPA annotated DTOs to work with persistence. Such DTOs will match db table and will be used within a custom repository that

  • will expose to clients a semantic API
  • will run queries against such DTOs
  • will use factories to initialize domain objects to return to clients

This approach make domain models truly decoupled from the db schema so that you can evolve both independently. The cons of this approach is that you have more code to write, but I find such code quite easy and cheap to write an maintain.

Using JPA annotation on entities is a quite common approach, but as your domain model evolves and becomes more complex, my own experience is that you have to face with problems that are far more expensive than the other solution.



来源:https://stackoverflow.com/questions/15837567/is-it-a-good-practice-to-use-jpa-entities-as-domain-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!