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!
2 Answers
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.

- 7,144
- 3
- 31
- 48
-
1Do you have a public Project with that implementation ? – heat Feb 17 '15 at 04:05
-
Using Hibernate as JPA provider allows you to map domain objects in XML. This can help you keeping the domain objects clean from JPA annotations. – Jonas Pedersen Jul 31 '15 at 12:32
-
That's exactly what I do too. Makes sense ;) – Mik378 Sep 23 '17 at 19:48
I think it's not a good idea, domain model should be not a JPA/Hibernate entity. With Domain Model you can change infrastructure database without changing domain model.
Some times we don't need the architectue hexagonal so you can chose another architeture if you code is verry simple.
Think about your application, and conplexity before choosen architeture hexagonal.

- 48
- 5