File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes JOINED strategy not sufficient? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JOINED strategy not sufficient?" Watch "JOINED strategy not sufficient?" New topic

JOINED strategy not sufficient?

Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
Hi ranchers,

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!

SCJP 1.5, SCWCD 1.4, SCBCD 5
Ran Pleasant
Ranch Hand

Joined: Jan 16, 2003
Posts: 76

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.

Ran Pleasant

Tim Storms
Ranch Hand

Joined: Apr 27, 2006
Posts: 64
Hi Ran,

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?

[Thumbnail for schema.GIF]

I agree. Here's the link:
subject: JOINED strategy not sufficient?
It's not a secret anymore!