Reviews : Cultivating your relationship with Castor-JDO :

To handle the three objects we have coded three Java-classes:
  • Media
  • Movie
  • Actor

As you may recall from the last article these classes are simple beans where the "many"-relations are implemented using Collections. To simplify adding and removing an element to a Collection we've coded add- and remove-methods. When Castor adds or removes elements to the Collections it will use these methods and also the set-methods. To see what's going on inside the classes I've added some System.out lines in the code. If you prefer using a logging utility like Log4J or the Logging API in JDK1.4 feel free to do so.

The Media class now looks like this:

package hansen.playground;

import java.util.*;

public class Media {

  private int id;
  private String type;
  private Collection movies = new ArrayList();
  
  public Media() {}
  public Media(int id, String type) { 
    this.id = id; this.type = type;}
  
  public int getId() { return id; }
  public void setId(int id) { this.id = id; }

  public String getType() { return type; }
  public void setType(String type) { this.type = type; }

  public Collection getMovies() { return movies; }
  public void setMovies(Collection movies) { 
    System.out.println(
      "***Media.setMovies: " 
      + movies.size()
      + " movies to media " 
      + this);
    this.movies = movies; 
  }

  public void addMovie(Movie movie) {
    System.out.println(
      "***Media.addMovie: " 
      + movie 
      + " to media " 
      + this);
    movies.add(movie);
  }

  public void removeMovie(Movie movie) {
    System.out.println(
      "***Media.removeMovie: " 
      + movie 
      + " from media " 
      + this);
    movies.remove(movie);
  }

  public String toString() {
    return "\"" + this.type + "\"";
  }  
  
}

- Listing 1: The Media class -

Note that all output from the add-, set-, and remove-methods are prefixed with "***", so they're easy to spot. Our first exercise is to run the ReadActor program from the last article. It lists the movies where Keanu Reeves acts.

The output looks like this:

***Media.addMovie: "The Matrix" to media "DVD"
***Movie.addActor: "Keanu Reeves" to movie "The Matrix"
***Actor.addMovie: "The Matrix" to actor "Laurence Fishburne"
***Movie.addActor: "Laurence Fishburne" to movie "The Matrix"
***Actor.addMovie: "The Matrix" to actor "Keanu Reeves"
***Media.addMovie: "Chain Reaction" to media "VHS"
***Movie.addActor: "Keanu Reeves" to movie "Chain Reaction"
***Actor.addMovie: "Chain Reaction" to actor "Keanu Reeves"
Movies starring Keanu Reeves
Movie: The Matrix
  Media: DVD
    Actor: Keanu Reeves
    Actor: Laurence Fishburne
Movie: Chain Reaction
  Media: VHS
    Actor: Keanu Reeves

- Listing 2: Output from ReadActor -

Since the record with Keanu Reeves has relations to all the other entities in the database Castor will fetch all data from the database and create 6 beans: 2 media-, 2 actor-, and 2 movie-beans. One interesting observation is Castor uses the add- methods to insert data in the Collections. What if we haven't coded add-methods? Let's temporarily remove the addMovie method in class Media, and run the program again:

***Media.setMovies: 1 movies to media "DVD"
***Movie.addActor: "Keanu Reeves" to movie "The Matrix"
***Actor.addMovie: "The Matrix" to actor "Laurence Fishburne"
***Movie.addActor: "Laurence Fishburne" to movie "The Matrix"
***Actor.addMovie: "The Matrix" to actor "Keanu Reeves"
***Media.setMovies: 1 movies to media "VHS"
***Movie.addActor: "Keanu Reeves" to movie "Chain Reaction"
***Actor.addMovie: "Chain Reaction" to actor "Keanu Reeves"
. . .

- Listing 3: Output from ReadActorforcing Castor to use set-methods -

As you can see Castor switches over to using the setMovies method, which must be present in the class to be a bean. If you're interested in changing this priority you may define the field for the Movies Collection like this in the mapping.xml file:

<field name="movies" type="hansen.playground.Movie" 
  collection="collection" 
  set-method="setMovies" 
  get-method="getMovies">

How to Add Java Applets to Your Site

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.