I am currently working on a Spring Roo/JPA application and I recently switched to a domain model based upon JPA inheritance. To sum up my domain model, I have an abstract Member entity that is subclassed by two entities: Male and Female.
In all of my Spring MVC controllers, I would like to avoid always injecting two service dependencies (one that would deal with Male entities and the other with Female entities). I’d rather have one MemberService service (together with a corresponding MemberRepository) that would return either a Male instance or a Female instance.
Is the above possible using Spring Roo annotations such as this one:
@RooService(domainTypes = { Member.class })or that one:@RooJpaRepository(domainType = Member.class)?
For instance, will this Member findByEmail(String email); return an object that I can cast to either the Female or Male type?
More generally, what are the best practices to deal with entity inheritance at the level of Spring controllers and Spring services?
I think the best way is to provide a repository for Male and another for Female
using:
you get:
then you create the service layer for the application for these models (a service for each model)
and then you get:
This is assuming that your Male and Female objects have different responsibility so is needed to separate it in different classes, by the name you gave them (Male and Female) i think they don’t, but i don’t know your business logic and of course this is just an example (but how faithful is to your real problem?).
The idea of inheritance is not only to share common functionality to “is-a” descendants but preserve the sense of the OO logic, when i say that i mean, in this case for example, you could resolve the problem with an attribute called sex if Male and Female have the same attributes; if this is not the case then wont be painful to create different repositories and services for those specialized objects.