Turbine

Essentials

Models

Get Involved

Documentation

Database

Advanced Peer Techniques

In this doc I'm going to try and explain a bit more about using Peers than just run of the mill selects, inserts and updates. However, this is by no means the be-all and end-all of Peer usage. It is just some ideas that I have found to work well.

Subclassing

Usually when we begin to extra code for Peer classes we end up with the problem of where to add the code. If we add it to the generated Peer classes we will loose everything if (and trust me this does happen) we need to regenerate the classes.

The solution is to create new classes that inherits from the generated classes and to add the extensions here. This has the benefit that we can generate new classes at any time and still maintain any changes we added.

To subclass successfully we need two new subclasses for each table. One for the data object and one for the peer class. Very Important: you need to override the doSelect() method of the Peer class to make sure that it creates objects of your new data object. If you don't do this you will still end up with instances of the original class and not your new class.

Using the example in the Peers docs we are going to create new classes for the Category and Item tables.

     public class MyCategoryPeer extends CategoryPeer
     {
        static public Vector doSelect (Criteria criteria) throws Exception
        {
            // Be sure to add the new class here
            return doSelect (criteria,"com.mycompany.om.MyCategory",null);
        }
     }

     public class ItemPeer extends ItemPeer
     {
        static public Vector doSelect (Criteria criteria) throws Exception
        {
            // Be sure to add the new class here
            return doSelect (criteria,"com.mycompany.om.MyItem",null);
        }
     }
   
     public class MyCategory extends Category
     {
     }

     public class MyItem extends Item
     {
     }
   

That is all there is to it, now we can start to write some code.

Useful Methods

I found that it saves a bit of duplicate work if you add some utility methods to your Peer class. The first method is very straightforward. It is used to select all the entries from a table and I usually call it doSelectAll().

     public class MyCategoryPeer extends CategoryPeer
     {
        static public Vector doSelect (Criteria criteria) throws Exception
        {
            // Be sure to add the new class here
            return doSelect (criteria,"com.mycompany.om.MyCategory",null);
        }

        static public Vector doSelectAll() throws Exception
        {
            Criteria crit = new Criteria();
            return = doSelect(crit);
        }
     }
   

The next method is used to select all the objects of a database relation. For example the relation between a category and items. Say for example that you routinely need to select all the items of a given category. We add a doSelectForCategory to the ItemPeer class.

     public class MyItemPeer extends ItemPeer
     {
        static public Vector doSelect (Criteria criteria) throws Exception
        {
            // Be sure to add the new class here
            return doSelect (criteria,"com.mycompany.om.MyCategory",null);
        }

        static public Vector doSelectForCategory(int categoryid) throws Exception
        {
            Criteria crit = new Criteria();
            crit.add (CATEGORY_ID,categoryid);
            return doSelect (crit);
        }
     }
   
Joins and linking objects

Sometimes you would like to have relations between tables to be available in the Peer objects. For example you want the category of an item to be available through a getCategory() method.

The first thing we need to do is to add getCategory() and setCategory() methods to the MyItem class.

     public class MyItem extends Item
     {
        private MyCategory category;

        public MyCategory getCategory()
        {
            return this.category;
        }

        public void setCategory (MyCategory category)
        {
            this.category = category;
        }
     }
   

Next we need to create a doSelectWithCategory() method for the ItemPeer class that creates the join between tables and inserts the category. We do it like this:

     public Vector doSelectWithCategory (Criteria crit) throws Exception
     {
        crit.addJoin (ItemPeer.CATEGORY_ID, CategoryPeer.CATEGORY_ID);

        addSelectColumns ( criteria );
        CategoryPeer.addSelectColumns ( criteria )

        // BasePeer returns a Vector of Value (Village) arrays.  The array
        // order follows the order columns were placed in the Select clause.

        Vector rows = BasePeer.doSelect(criteria);

        Vector results = new Vector();

        // populate the object(s)
        for ( int i=0; i<rows.size(); i++ )
        {
            Record row = (Record)rows.elementAt(i);

            MyItem itm = row2Object (row,
                                     1,
                                     Class.forName
                                     ("com.mycompany.om.MyItem"))

            MyCategory cat = CategoryPeer.row2Object
                                    (row,
                                    numColumns+1,
                                    Class.forName
                                    ("com.mycompany.om.MyCategory"))

            itm.setCategory (cat);
            results.add (itm);
         }

         return results;

     }
   

Copyright © 1999-2001, Apache Software Foundation