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.

 Microsoft Visual Studio 2010 Showcase
 Avaya Developer Showcase
 MSDN Spotlight
 PHP for Windows Showcase
XML error: undefined entity at line 39
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%.

Windows 7: From Beta to Final Code in One Year
Google Shows Off Chrome OS, Releases Source
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?
Fedora 12 Takes Aim at Linux Networking
Top Supercomputer Nearly Doubles in Speed
Fedora 12 Linux Tackles Virtualization
Apple Gives iPhone Developers App Status Tracker
Novell Sets OpenSUSE 11.2 Free

Creating Custom Export Filters for StarOffice with XSLT
WPF Wonders: Using DataTemplates
Crystal Reports Family Offers Options for Developers
Avaya Aura Session Manager video
Avaya Aura Overview video
Exploring HTML 5's Audio/Video Multimedia Support
Overriding Virtual Functions? Use C++0x Attributes to Avoid Bugs.
Understanding the Cloud Computing Security Vulnerabilities
Cisco and IBM Target a Greener World
Upgrade to Visual Studio 2010 with the Ultimate Offer

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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs