HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Java Data Object"
Vol. 8, Issue 3, p. 36

	


Listing 1   Metadata for my example

 1 <?xml version="1.0"?>
 2 <jdo>
 3   <package name="whs.jdo">
 4    <class name="Book" 
        identity-type="application"
        objectid-class="BookKey">
 5       <field name="nm" 
          primary-key="true" />
 6    </class>
 7   
 8    <class name="Block" >
 9      <field name="id"  
         persistence-modifier="none" />          
10      <field name="children">
11         <collection 
            element-type="Block"/>
12      </field>
13      <field name="attributes">
14        <map key-type="String"/>
15        <map value-type="String"/>
16      </field>                       
17    </class>
18   </package>
19 </jdo>

Listing 2  Properties for setting up JDO

1  javax.jdo.option.ConnectionUserName=
      Database user
2  javax.jdo.option.ConnectionPassword=
      Password of the user
3  javax.jdo.option.ConnectionURL=
      URL of the database
4  javax.jdo.option.ConnectionDriverName=
     Classname of JDBC driver 
5
6  javax.jdo.PersistenceManagerFactoryClass=
      com.solarmetric.kodo.impl.jdbc.
      JDBCPersistenceManagerFactory
7
8  javax.jdo.option.Optimistic=true
9  javax.jdo.option.RestoreValues=true
10 javax.jdo.option.RetainValues=false

Listing 3   JDO Code Fragment

1  //  --- Get persistence manager ---
2  PersistenceManagerFactory  factory = 
   JDOHelper.getPersistenceManagerFactory
     (property);
3  PersistenceManager pm = 
      factory.getPersistenceManager()
4
5  // --- Add ---
6  pm.currentTransaction().begin ();
7  pm.makePersistent (obj);
8  pm.currentTransaction().commit ();
9
10 // --- Iterate Extent & Update ---
11 pm.currentTransaction().begin ();
12 Extent ext =  pm.getExtent 
      (MyClass.class, false);
13 for (Iterator i = ext.iterator ();  
       i.hasNext();) {
14   MyClass obj = (MyClass)i.next ();
15   obj.setField1("AA");
16 }
17 pm.currentTransaction().commit ();
18
19
20  // --- Query and Delete ---
21  pm.currentTransaction().begin ();
22  String filter ="nm=\"JDO Book\"";
23  Query qry= pm.newQuery(ext, filter);	
24  Collection c =
          (Collection) qry.execute();
25  Object obj = c.iterator().next();
26  pm.deletePersistent (obj);
27  pm.currentTransaction().commit();
28
39  // --- Close resources ---
30  pm.close();

Listing 4 Book & Block object

1  class Book {
2    String nm;
3    Block block= new Block("Document");
4
5    Book(String name){
6        this.nm = name;
7    }
8
9    void addChild(Block e)  {
10      block.addChild(e);
11   }
12  }
13
14  class Block {
15    String type;
16    Integer id;
17    Map  attributes = new HashMap();
18    List children= new ArrayList();		
19	
20    Block(String type)   {
21       this.type = type;
22    }
23   
24    void addChild(Block e) {
25      children.add(e);
26    }
27  
28    void setAttribute(String key, 
                        String attr){
29        attributes.put(key, attr);
30   }
31 }

Listing 5  Create Test Book

1  Book book = new Book("Intro to JDO");
2  Block chp1= new Block
	("Overview", "Chapter");
3  Block sec11 = new Block
	("Advantage of JDO", "Section");
4  chp1.addChild(sec11);
5  Block chp2 = new Block
       	("Example", "Chapter");
6  chp2.setAttribute("Color", "Red");
7  Block sec21 =new Block
       	("JDBC Code", "Section");
8  Block sec22 = new Block
       	("JDO Code", "Section");
9  chp2.addChild(sec21);
10 chp2.addChild(sec22);
11 book.addChild(chp1);
12 book.addChild(chp2);

Listing 6 BookKey.java

1  public final class BookKey  {
2    public String nm = null;
3    public BookKey(){}
4
5    public BookKey(String nm) {
6       this.nm = nm;
7    }
8
9    public boolean equals (Object o){
10     if (o == this)   return true;
11     if (!(o instanceof BookKey))  
           return false;
12     return((BookKey)o).nm.equals(nm);
13   }
14
15   public int hashCode () {
16    return nm.hashCode();
17   }
18
19   public String toString() {
20    return nm;
21   }
22 }

Listing 7 BookPersistJDO.java

1  class BookPersistJDO {
2
3   public Book addBook (Book book)  
4   throws JDOUserException {
5     PersistenceManager pm =getPM(); 
6     Transaction tran = 
          pm.currentTransaction ();
7     tran.begin ();
8     pm.makePersistent (book);
9     tran.commit ();
10    return book;
11  }
12 
13  public void deleteBook (Book book){
14    PersistenceManager pm =getPM();
15    Transaction tran =  
        pm.currentTransaction();
16    tran.begin ();
17    pm.deletePersistent (book);
18    tran.commit ();
19  }
14
15  public Collection  getAllBooks() {
15    PersistenceManager pm = getPM ();				
17    String filter ="";
18    Extent extent =pm.getExtent  
            (Book.class,  false);
19    Query qry = pm.newQuery 
            (extent, filter);
20    return (Collection)qry.execute();
21  }
22
23  public Book  getBook(String name) {
24    PersistenceManager pm = getPM ();				
25    String filter="nm==\"" + 
                    name  +"\"";
26    Extent extent = pm.getExtent 
              (Book.class,  false);
27    Query qry = pm.newQuery 
              (extent, filter);
28    Collection c = 
            (Collection) qry.execute ();
29    return (Book) c.iterator().next();
30   }
31 }

Additional Code For This Article (~ 7.78 KB ~zip format )
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.