Handling one-to-many relations
Consider a collection of movies. Each is either in VHS or DVD format:
The steps to add this relation to the object model, the database model, and
the Castor setup are these:
A. Java classes
The Movie class looks like this:
package hansen.playground;
import java.util.*;
public class Movie {
private int id;
private String title;
private Media media;
public Movie() {};
public Movie(int id, String title) { this.id = id;
this.title = title;};
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public Media getMedia() { return media; }
public void setMedia(Media media) { this.media = media; }
}
- Listing 4: The Movie class -
In Media you add a Collection to keep the movies in:
private Collection movies = new ArrayList();
public Collection getMovies() { return movies; }
public void setMovies(Collection movies) {
this.movies = movies;
}
public void addMovie(Movie movie) {
movies.add(movie);
}
B. Database tables
Create the movie table:
CREATE TABLE movie (
id int NOT NULL,
id_media int NOT NULL,
title varchar(100),
PRIMARY KEY (id)
)
id_media is the foreign key to
the media table. The key, id, may be generated automatically by
MySQL (and many other database systems), but to keep the examples vendor
neutral, I've chosen not to use this feature in this article.
C. The mappings file
Modify the mapping.xml file:
<class name="hansen.playground.Movie" identity="id">
<map-to table="movie" />
<field name="id" type="integer">
<sql name="id" type="integer"/>
</field>
<field name="title" type="string">
<sql name="title" type="varchar" />
</field>
<field name="media" type="hansen.playground.Media" required="true">
<sql name="id_media" />
</field>
</class>
<class name="hansen.playground.Media" identity="id">
<map-to table="media" />
<field name="id" type="integer">
<sql name="id" type="integer"/>
</field>
<field name="type" type="string">
<sql name="type" type="char" />
</field>
<field name="movies" type="hansen.playground.Movie" collection="collection">
<sql many-key="id_media" />
</field>
</class>
This is the most interesting part of the setup. The new things are these:
- the Movie object refers to the Media object by giving the
name of the class as the type.
We want a media for every movie hence
required=true.
- the Media object also refers to the Movie objects by giving
the class name and also the kind of Java object that is used to hold the
movies.
There is no field in the media table that corresponds to
movies, instead we use the many-key attribute to give Castor the
name of the field that holds the foreign key in the Movie
object.
D. Test the setup
Let's assume that we have two records in the Media table:
Here's a program that creates three movies, two in DVD format, one in
VHS:
package hansen.playground;
import org.exolab.castor.jdo.*;
import java.util.*;
public class CreateMovies {
public static void main(String[] args) {
try {
// Define the JDO object
JDO jdo = new JDO("mydb");
jdo.setConfiguration("database.xml");
// Open a connection to the database
Database db = jdo.getDatabase();
// Begin a transaction
db.begin();
// Load the DVD and VHS objects using their keys
Media DVD = (Media)db.load(Media.class,new Integer(1));
Media VHS = (Media)db.load(Media.class,new Integer(2));
Movie m1 = new Movie(11, "The Matrix");
m1.setMedia(DVD);
DVD.addMovie(m1);
db.create(m1);
Movie m2 = new Movie(12, "Lord of the Rings");
m2.setMedia(DVD);
DVD.addMovie(m2);
db.create(m2);
Movie m3 = new Movie(13, "Chain Reaction");
m3.setMedia(VHS);
VHS.addMovie(m3);
db.create(m3);
// Commit the transaction
db.commit();
db.close();
} catch (PersistenceException e) {
e.printStackTrace();
}
}
}
- Listing 5: Testprogram for the Movie class
-
If we now look into the movie table we'll find these three
records:
id |
id_media |
title |
11 |
1 |
The Matrix |
12 |
1 |
Lord of the Rings |
13 |
2 |
Chain Reaction
|
You'll notice that the keys for the DVD media (1) and VHS (2) have been
inserted in the foreign key fields.
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.
|