Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When working with annotations on entities in JPA, do these entities belong to the model layer or the persistence layer?

Does there have to be some kind of clone of the entities (simple JavaBeans) in the model layer so it could be decoupled entirely from JPA?

What is the best practice here?

share|improve this question
up vote 4 down vote accepted

The correct answer on "best practice" questions is always - it depends on the context.

I would start simple by putting JPA entities into Model layer and using them in Persistence layer. This will serve you well for small, medium and even most large projects. If the time comes when you need to have separate entities/value objects/transfer objects for each layer (don't forget UI!), the refactoring is not going to be insurmountable. One way to buy affordable insurance now is to use DAO pattern for all persistence concerns. Looks like you're already doing it because you mentioned Persistence layer.

share|improve this answer
Yes, I use a persistence layer in order to call my named queries contained in various entities, these ones owned by a Domain Layer (also called Model Layer). The drawback is that both, domain and persistence layers, have to depend on JPA API and thus Hibernate (provider in my case) – Mik378 Feb 8 '12 at 19:29
It's a more philosophical drawback than practical. In practice you're getting lots of simplification for your solution by depending on JPA API (not Hibernate!). – Yuriy Zubarev Feb 8 '12 at 19:33
Yes effectively, my Domain Layer depends on JPA only not Hibernate like my persistence layer do. – Mik378 Feb 8 '12 at 19:45
@Mik378: If you can test the domain layer without a database, then the dependence is more theoretical than practical. I prefer to think that the addition of JPA annotations allow the domain layer to support persistence, but does not create a dependency on persistence. – kevin cline Feb 8 '12 at 23:26
@kevin cline Indeed, it is practical since JPA entities are POJO :) – Mik378 Feb 8 '12 at 23:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.