adam bien's blog

15 Tips on JPA Rich Domain Modelling - Thinking In Objects - Not Mappings [For Greenfields] 📎

  1. Forget about JPA, JDBC & CO for a moment.
  2. Try to identify concepts and abstractions from the target domain. Use them as candidates for entities.
  3. Reuse directly the names. Do not obfuscate them with additional technical naming conventions like XYZEntity or "BOs".
  4. Forget the database mapping and concentrate on good, DRY OO-design and fluent naming.
  5. Build objects, not structs - put logic into the entities if necessary.
  6. Forget getters / setters. Think about builders.
  7. Write unit tests for the business logic and run them, without involving the EntityManager etc. [optional - only if your logic in the entities becomes complex]
  8. Let the tool (JPA-provider) generate the DDL and the database layout.
  9. Write unit tests, without mocking out the database, early.
  10. Provide a lean facade/service layer (the easiest possibility are EJB 3 right now). It should only contain crosscutting concerns.
  11. Run load and stress tests.
  12. Import the database into an ER-Tool - optimize the DDL.
  13. Align the JPA-annotation, if needed.
  14. Provide orm.xml if required (you should have good reasons for it).
  15. Go to 11.
[Rich and anemic domain objects are discussed in-depth in the "Real World Java EE Patterns" book. See e.g. page 83 and 259]