Looking at relations
When I started to work with Castor I remember that I had a hard
time figuring out what Castor would do for you regarding linking
of related Java objects. Let's look at an example. If I create a
new movie and add an existing actor to the movie's Collection of
actors, do I then need to add the new movie to the actor's
Collection of movies? If you weren't working with Castor you'd
of course have to do it, but from the mapping.xml file
Castor "knows" how a movie and actor are related. So I'd guess
that Castor would be able to add the movie to the actor object
for us.
This is not an issue regarding if data is persisted correctly on
the database. When I add the actor to the movie and use
commit it will be reflected correctly in the "bridge"
table that represents the many-to-many relation between the
actor and movie. So my curiosity is merely about how the
consistency of the Java object model is handled.
Let's make an experiment. I've created a new movie, "Dirty
Harry", and a new actor, "Clint Eastwood". Now I want to tie the
movie to the actor, but only at one end:
actor.addMovie(movie);
To see if Castor does anything to the Movie object, we'll
list the details for the movie before and after commit.
To ease the testing we need some utility methods for listing
different media, movies and actors. I've coded them as static
methods in a separate class called ListUtility.The output from this class is prefixed
with "===".
The main part of the
Dirty Harry program looks like this:
. . .
ListUtility.showMovie(movie);
System.out.println("---> Now adding movie " + movie.getTitle()
+ " to actor " + actorName);
actor.addMovie(movie);
System.out.println("---> After add");
ListUtility.showMovie(movie);
// Commit the transaction
db.commit();
db.close();
System.out.println("---> After commit");
ListUtility.showMovie(movie);
. . .
|
|
When we run the program we get this output:
=== Show Movie
=== Title: Dirty Harry
=== Media: VHS
=== Actors (0):
---> Now adding movie Dirty Harry to actor Clint Eastwood
***Actor.addMovie: "Dirty Harry" to actor "Clint Eastwood"
---> After add
=== Show Movie
=== Title: Dirty Harry
=== Media: VHS
=== Actors (0):
---> After commit
=== Show Movie
=== Title: Dirty Harry
=== Media: VHS
=== Actors (0):
|
|
When looking into the database (the "bridge" table) we notice
that the relation has been established, but it's obvious that
we'll have to add the actor to the movie to have my Java objects
connected properly:
movie.addActor(actor);
One solution could be to call addActor from addMovie and vice
versa. I'll return to this option later.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.