Lightweight R/O Mapping

Lightweight R/O Mapping

Here is the enhanced Jedi class. It just has an additional member of the type List<Fighter>. Again, this is J2SE 5.0 code that states that the list contains only objects of the type Fighter. Added code appears bold.



public class Jedi {



   private Integer _id;

   private String _name;

   private Double _forceRating;

   private Integer _age;

   private Boolean _alive;

   

   private ArrayList<Fighter> _fighterList = 

      new ArrayList<Fighter>();

   

   @ColumnAssociation(name="jedi_id")

   public void setId( Integer id ) {

      _id = id;

   }

   @ColumnAssociation(name="name")

   public void setName( String name ) {

      _name = name;

   }

   @ColumnAssociation(name="force_rating")

   public void setForceRating( Double forceRating ) {

      _forceRating = forceRating;

   }

   @ColumnAssociation(name="age")

   public void setAge( Integer age ) {

      _age = age;

   }

   @ColumnAssociation(name="alive")

   public void setAlive( Boolean alive ) {

      _alive = alive;

   }



   @ParameterAssociation(name="@jedi_id", 

      index=0, isAutoIdentity=true)

   public Integer getId() {

      return _id;

   }

   @ParameterAssociation(name="@name", index=1)

   public String getName() {

      return _name;

   }

   @ParameterAssociation(name="@force_rating", 

      index=2)

   public Double getForceRating() {

      return _forceRating;

   }

   @ParameterAssociation(name="@age", index=3)

   public Integer getAge() {

      return _age;

   }

   @ParameterAssociation(name="@alive", index=4)

   public Boolean getAlive() {

      return _alive;

   }

   public ArrayList<Fighter> getFighterList() {

      return _fighterList;

   }

   public void setFighterList( ArrayList<Fighter> fighterList ) {

      _fighterList = fighterList;

   }

}

The code that reads the available Jedis from the database now looks like this:



Connector connector = 

   ConnectorFactory.getInstance().createConnector( "starwars" );

BeanReader jediReader = 

   new BeanReader( Jedi.class, connector );

BeanReader fighterReader = 

   new BeanReader( Fighter.class, connector );

Collection<Jedi> jediList = 

   reader.executeCreateBeanList( "select * from jedi" );

for( Jedi jedi : jediList ) {

   String query = 

      "select * from fighter where jedi_id = " + jedi.getId();

   Collection<Fighter> fighters = 

      fighterReader.executeCreateBeanList( query );

   jedi.setFighterList( 

      new ArrayList<Fighter>( fighters ) );

}

Et voila, there you have all of the Jedi with a collection of their respective fighters. Note that we have not coded the reading of the Fighter collection into the Jedi class. This would mean tight coupling between the Jedi and the Fighter. You could say that the code above is the assembler part in a dependency injection pattern. Big words, I know. It just means this: Do things that depend on each other separately, and then put them together in separate code. If you want to read a very good summary on this topic see Martin Fowler's "Inversion of Control Containers and the Dependency Injection pattern."

Prev  [1] [2] [3] [4] [5] [6] [7] Next

Close    To Top
  • Prev Article-Java:
  • Next Article-Java:
  • Now: Tutorial for Web and Software Design > Java > JDOnJDBCnSQLJ > Java Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction