This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I'm struggeling with a mapping strategy for a couple of entities. Note that I'm using JPA 2.0 and Hibernate 3.5.6. The application serves as a learing app for this frameworks. This is the scenario I've implemented:
- a person can be an actor or a director
- both of the previous types can participate in one or multiple (different) movies
- a movie can include one or multiple actors and one or more directors
- and what's giving me a headache: an actor and a director can be one and the same person!
This is what my mapping is looking like:
I've not included the getters and setters, plus the builder classes to easily create a director or an actor. Just for now, suppose I've created an actor like this:
As this is the first entity to be saved, an identifier with value 1 will be generated. Now here comes the part I'm struggling with. This actor should also be known as an director of several other movies. The (dirty) solution I was thinking about, was to copy all the values of the actor into the director, including the identifer. But because the identifier is auto generated when an entity is persisted, the given identifier will be overridden by a new generated id. Thus the director and the actor are two different persons in the database. Does anyone know a clean sollution for this? Should I map my entities differently? Should I choose another id generation strategy? Thanks for sharing your thoughts!
I think the problem is in the inheritance from Person. Director and Actor are not types of Person, rather they are roles that a person can fulfill. A solution would be to create an abstract class named Role and define both Director and Actor as subclasses of Role. The Person class would then have a list of Roles that they fulfill.
Joined: Apr 27, 2006
Thanks for your reply. You object structure suggestion seems to be better than mine. I've quickly created the new model classes. This is what they look like:
The attachment contains the generated database schema.
Do you still see some improvements, or is it fine as it is?