Here's a program that creates three actors and add them to the movies:
package hansen.playground;
import org.exolab.castor.jdo.*;
import java.util.*;
public class CreateActors {
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();
OQLQuery query;
QueryResults results;
query = db.getOQLQuery(
"SELECT a FROM hansen.playground.Movie a WHERE a.id=11");
results = query.execute();
Movie Matrix = (Movie)results.next();
query = db.getOQLQuery(
"SELECT a FROM hansen.playground.Movie a WHERE a.id=12");
results = query.execute();
Movie Rings = (Movie)results.next();
query = db.getOQLQuery(
"SELECT a FROM hansen.playground.Movie a WHERE a.id=13");
results = query.execute();
Movie Chain = (Movie)results.next();
Actor a1 = new Actor(21, "Keanu Reeves");
a1.addMovie(Matrix);
a1.addMovie(Chain);
db.create(a1);
Matrix.addActor(a1);
Actor a2 = new Actor(22, "Laurence Fishburne");
a2.addMovie(Matrix);
db.create(a2);
Matrix.addActor(a2);
Actor a3 = new Actor(23, "Ian Holm");
a3.addMovie(Rings);
db.create(a3);
Rings.addActor(a3);
// Commit the transaction
db.commit();
db.close();
} catch (PersistenceException e) {
e.printStackTrace();
}
}
}
- Listing 7: Testprogram for the Actor class
-
Note, that I've used an alternate way of loading the objects, using OQL, the
Object Query Language. OQL looks a lot like SQL, but uses Java class names in
stead of table names.
The actor table now contains
id |
name |
21 |
Keanu Reeves |
22 |
Laurence Fishburne |
23 |
Ian Holm |
The actor_movie table contains:
id_actor |
id_movie |
21 |
11 |
21 |
13 |
22 |
11 |
23 |
12 |
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.
|