Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

entity bean relationships

420 Chapter 7
entity bean relationships
you are here 4 421
Director 2
Relationship assignments
A Movie can have only one Director. If you set a
Movies Director feld to Director 1, the Movie
will be in Director 1s collection of Movies, but
in no other Directors collection. If you later
reassign the Movies Director feld so that the
Movie has a different DirectorDirector 2the
previous Director (Director 1) will no longer
have that Movie in its Collection.
This all happens automatically! To maintain the
integrity of the database, the Container manages
both sides of the relationship at all times.
I directed that
movie! That movie
is MINE. It belongs in
MY Collection.
No, that movie
should be MINE. If
a Movie can have only
one Director, then
Im it!
movieA.setDirector(movieD.getDirector());
Before:
Director 1
MovieA
MovieB
MovieC
Three Movies in Director
1s Movie Collection. If you
call getDirector() on any of
these Movies, they will return
Director 1.
MovieD
There is only one Movie
in Director 2s Collection.
Its the only Movie that
will return Director 2 if
you call getDirector() on
the Movie.
Director 2
After:
Director 1
Movie A can have only one Director, so when
we reassigned the Director for Movie A to
be Director 2, Movie A was automatically
REMOVED from Director 1s Collection of
Movies. Remember, the Container manages
both sides of the relationship!
When we reassigned the
Director of Movie A, that Movie
automatically moved into Director
2s Collection of Movies.
Director 2
this says, set Movie As
Director to be whatever the
Director is for Movie D.
Director 1
MovieD
MovieA
MovieB
MovieC
MovieA
422 Chapter 7
entity bean relationships
you are here 4 423
If the mul tiplici ty of the relationship field is ONE, i ts a MOVE
If the mul tiplici ty is MANY, i ts a COPY
When we assigned Director 2 to Movie A, the Movie moved from Director 1s Collection to
Director 2s Collection. The Director CMR feld has a multiplicity of One in its relationship
with Movie, so a Movie cant exist in the Collection of two different Directors, because a
Movie can have only One Director.
But just because we move the Movie, doesnt mean the Director also moves. The
multiplicity of Movie is Many, so when you assign another Movies Director to a different
Movie, that Director reference is copied, and both Movies will now have a reference.
Because the Director-to-Movie relationship is One-to-Many, a
Movie can NEVER exist in more than one Directors Collection. If
you change a Movies Director, the Movie is not copied! It is moved
to the new Directors Collection.
<ejb-relationship-role-name>DirectorBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<ejb-relationship-role-name>MovieBean</ejb-relationship-role-name>
<multiplicity>Many</multiplicity>
Director 1
Director 2
movieA.setDirector(movieD.getDirector());
MovieD
MovieB
MovieC
MovieA
The MOVIE reference was MOVED from one Director to another
The DIRECTOR reference was COPIED from one Movie to another
MovieA
Director 2
MovieD
Director 2
MovieD
MovieA
assignments in relationships
Movie Ds
reference was
copied, not moved.
422 Chapter 7
entity bean relationships
you are here 4 423
<ejb-relationship-role-name>TrailerBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<ejb-relationship-role-name>MovieBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
Movie 1 Movie 2
movie1.setTrailer(movie2.getTrailer());
TrailerA
TrailerB
And this current scenario:
Given this relationship:
Draw what the picture will look like after you run:
Draw your
picture here...
Answer these questions:
What is the result of: movie1.getTrailer()
What is the result of: movie2.getTrailer()
What is the result of: trailerA.getMovie()
What is the result of: trailerB.getMovie()
Exercise
Multiplicity and Assignments
424 Chapter 7
entity bean relationships
you are here 4 425
<ejb-relationship-role-name>TrailerBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
<ejb-relationship-role-name>MovieBean</ejb-relationship-role-name>
<multiplicity>One</multiplicity>
Movie 1 Movie 2
movie1.setTrailer(movie2.getTrailer());
TrailerA
TrailerB
And this current scenario:
Given this bi-directional relationship:
Draw what the picture will look like after you run:
Answer these questions:
movie1.getTrailer()
movie2.getTrailer()
trailerA.getMovie()
trailerB.getMovie()
Solution
Multiplicity and Assignments
Movie 1
TrailerA
Movie 2
TrailerB
null
null
returns trailerB
returns null! Movie has a multiplicity of One, so a Trailer can have
only ONE Movie. A Trailer cant be with both Movie 1 and Movie 2
also returns null... Trailer has a multiplicity of One, so there can
be only one Trailer per Movie. When TrailerB went to Movie 1,
TrailerA was kicked out, disconnected from the relationship.
returns Movie1, not that it has been assigned to Movie1, the Container
updates the feld returned from the Trailers getMovie().
exercise solution

You might also like