Lightweight R/O Mapping

Lightweight R/O Mapping

Step by Step

Let's tackle a little persistence problem. To read a list of Jedi objects from the database, we assume that the result set that is delivered looks like the table below. Please note that we are not going to rely on tables in the following discussion, even though the examples are actually based on tables. In general, we're just expecting tabular data that might have been put together using several SQL joins and spanning more than just one table or view. (Also, apologies to all Star Wars fans for any inconsistencies.)





jedi_id  name             force_rating  age  alive 

-------- ---------------- ------------- ---- ----- 

1000     Obi Wan Kenobi   9.40          30   0

1001     Luke Skywalker   7.20          19   1

1002     Yoda             10.00         912  1

Let's define a simple class called Jedi.



public class Jedi {



   private Integer _id;

   private String _name;

   private Double _forceRating;

   private Integer _age;

   private Boolean _alive;



   @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 fr ) {

      _forceRating = fr;

   }

   @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;

   }

}

What happened here? You see two kinds of annotations above the getter and setter methods of that class.

The annotation @ColumnAssociation is used to connect the setter methods of the JavaBean to a column from the result set, so that the tabular data from the database can be written to the bean properties by Amber. The annotation @ColumnAssociation applies to the setter methods only, because Amber uses these annotations to find and call those methods with the corresponding values after reading the data from the database.

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