Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 57 design patterns The cascading ALL relation causes all persistence operations that are invoked on the electric guitar to be cascaded to the body and its potential relations thus ensuring consistency of the involved entities ElectricGuitar represents an aggregate type Repositories All the mentioned persistence operations need to be invoked somehow In the same way domain entities need to be retrieved from a persistence provider in a consistent way DDD repositories are responsible for managing the persistence of domain entities They encapsulate this functionality in a self suficient and consistent way to keep the rest of the domain model clear of persistence implementation details Only entities that expose a unique identity within the business domain are persisted and managed via repositories In Java EE and JPA the provided EntityManager type fulfills this function already It is used to persist retrieve and manage domain objects that are defined as entities or object hierarchies thereof JPAs constraint that entities are required to define an identifier property fits the idea that DDD entities are identifiable within the business domain The entity manager is injected and used by services as follows @ Stateless public class InstrumentCraftShop @ Inject InstrumentMaker instrumentMaker @ PersistenceContext EntityManager entityManager public ElectricGuitar craftInstrument ElectricGuitar instrument instrumentMaker build return entityManager merge instrument
You must have JavaScript enabled to view digital editions.