| ★ wanayoo — archive 1999 http://java.webdeveloper.com/Map_Java_Objects/index-5.html | Nouvelle recherche | Portail wanayoo |
![]() | ||||||||||||||||||||||||||||||||
Reviews :Mapping Java Objects to a Database with Castor-JDO:Handling one-to-many relationsConsider 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:
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:
You'll notice that the keys for the DVD media (1) and VHS (2) have been inserted in the foreign key fields.
How to Add Java Applets to Your SiteNew on the Java Boutique:
New Article:
New Tutorial:
Year in Review: Elsewhere on internet.com:
WebDeveloper Java
WDVL Java
ScriptSearch Java |
| |||||||||||||||||||||||||||||||
![]() |
| |||||||