Complex inheritance schemes can cause a lot of problems when two objects share a same root, but diverge too much from each other.
Model
A model composed of:
Interface
JPA entity
DTO with additional fields
publicinterfaceModelObject {publicStringgetName();publicvoidsetName(finalString name);}@EntitypublicclassModelObjectEntityimplementsModelObject {// Class prepared for persistencepublicIntegergetId() {return id; }}publicclassModelObjectAdditionalFieldimplementsModelObject {// Implements the interface, but adds a fieldpublicIntegergetDate() {return date; }}
Service
/*** Service making use of the interface.* <p>* One method expects and returns the interface.* <p>* Another expects and returns an iterable of the interface.*/publicinterfaceModelObjectService { /** * Returns an object matching the received sample. * * @param sample the sample * @returns an object matching the sample */publicModelObjectfind(finalModelObject sample); /** * Returns an object matching the received sample. * * @param data objects to save * @return all the objects which could be saved */publicIterable<ModelObject> save(finalIterable<ModelObject> data);}