ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Lightweight R/O Mapping
Pages: 1, 2, 3, 4, 5, 6, 7

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."

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow