★ wanayoo — archive 1999 http://java.webdeveloper.com/Map_Java_Objects/index-5.htmlNouvelle recherche | Portail wanayoo
Computer Digital Expo

Tophosts.com - The World's #1 Hosting Resource & Directory


Find a Web Host With:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
Internet News
Internet Investing
IT
Windows Technology
Linux/Open Source
Developer
Interactive Marketing
xSP Resources
Small Business
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers
Be a Commerce Partner
Submit Your Site
Conference Calling
Find a Web Host
Compare Prices & Shop
IT Training
Build an Online Store
Business Search
Best Deals on PDAs!
Software Store
Send a Press Release
FlashKit
Gif.com
HierMenusCentral
JavaBoutique
JavaScript.com
JavaScriptSource
WebDev Jobs
JustSMIL
ScriptSearch
StreamingMediaWorld
Web Hosts
WebDevelopersJournal WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com
CGI
HTML
Java
JavaScript
Web Design and Promotion
Web Site Development
E-mail Address:


Can you receive
HTML e-mail?

Yes   No

Zip Code:



Reviews :

Mapping Java Objects to a Database with Castor-JDO:

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:

id
type
1
DVD
2
VHS

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.    


Applet Index
(sorted alphabetically)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z #s
The Java Source
(applets w/source code)

A B C D E F G H I J K
L M N O P Q R S T U
V W X Y Z

How to Add Java Applets to Your Site

New on the Java Boutique:

New Article:
J2EE Deployment Specification
Why is it that J2EE applications written in the same language, Java, may not coexist on different servers? While the answer isn't simple Sun has set out to alleviate these problems with the "J2EE Deployment Specification".
... [more articles]

New Tutorial:
Designing Packages for Stability
Are your designs stable? This month Samudra will focus on the change impact relationship involved in designing classes showing us how to take them into account when designing the package relationship.
... [more Tutorials]

Year in Review:
The Best of 2002
The year 2002 saw 201 new applets posted to JavaBoutique. Check out the 2 most popular apps from each month of 2002!
... [popular applets]

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.

internet.com logo






Developer News

Deadline Looms for IBM's AIX

HP, Dell Pledge Java Support

Putting Java in ONE Cup



Database Journal Launched!
Jupiter Media has announced the launch of Database Journal. DBJ offers SQL courses and other database related resources for beginner to expert developers.



PhotoMap
PhotoMap is made of many pictures and links. You can travel in "photo map world" where you have never visit yet.
Download of the Week
BSCOutline v5.10
BSCOutline Java Outline Applet is a customizable tree view control for use in Web pages, similar to that used by Windows File Manager or Explorer. BSCOutline has been written to be the simple to use and small to store.

JB User Poll
As a JavaBoutique Reader what kind of articles do you prefer?
The JavaBoutique Top 15:
1. ChompText
2. PingPong
3. DJPopMenu
4. NavBar
5. 3DMaze
6. RushHour
7. Nibbly
8. Encyclo
9. AnimLetters_anim
10. AScroll2
11. Viewer
12. Distorter
13. BMI
14. BMMenu
15. SonaliCalendar
Want more? Check out our Top 100!

Refer-It
Affiliate Program and Referral Directory.
New on internet.com
Are Open Source Databases Following in Linux' Footsteps?
All signs seem to point to open-source databases providers following the path of Linux in building momentum.

B-Blogs: Mega-Meta, Very Viral
Are you laid-back enough to b-blog?

A Primer to Active Directory
Active Directory is a potent tool for managing large networks of systems across WANs and LANs, but it can be complex to understand and administer. Beth Cohen's primer is designed to help you get acquainted with the tool that can help you create a robust and flexible architecture for addressing your company's needs.


Find the lowest price for a variety of products:


Featured Categories:
Computers
Digital Cameras
Home Theatre
Video Games
Laptops
Software
Electronics
PDA's

Jupitermedia is publisher of the internet.com and EarthWeb networks.
Copyright 2003 Jupitermedia Corporation All Rights Reserved.

Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/
http://www.earthweb.com/