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

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.

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

Next Pagearrow