advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

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.

 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 34
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Brings Developers Into the Cloud
Apache at 10: You Can't Buy Us
Microsoft's CodePlex Foundation Moving Forward
Apple Claims 100,000 Apps, Google Analyzes Them
Nokia Latest to Play Opera Mobile 10 Browser
PayPal Opens Up Payment Platform to Devs
Ubuntu Linux 9.10 'Karmic Koala' Starts Its Climb
IBM Links Rational Developer Tools, Tivoli Apps
Libraries Give Vista Apps a Windows 7 Look
Ubuntu: The 'Default Alternative' to Windows?

Delivering Web-based Embedded Fonts in CSS 3
Adobe Helps PHP Developers Create Rich Internet Applications
Java Developers Finding a Home at Adobe Flex
Virtualization Delivers a Dynamic Infrastructure
Consuming XML Web Services in iPhone Applications
Build a More Agile Business with IBM
POJO-Based Solutions for LDAP Access: One Good, One Better
IBM Offers Enhanced Measurement and Management for Energy Usage
IBM Helps Transformation to an Information-Based Enterprise
Top Five Touch UI-Related Design Guidelines

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs