Class AutoTable

java.lang.Object
onl.ycode.stormify.AutoTable

public abstract class AutoTable extends Object
A common abstract class to support auto-populating of fields.

This class supports the method autoPopulate() which is able to fetch the data from the database when triggered. It is still important to add the call to autoPopulate() before accessing (setting or getting) any of the fields that should be auto-populated.

The idea is, to trigger the call to the autoPopulate() method early, before accessing any fields, so when the fields are accessed, they are already populated.

The primary key fields are required to pre-exist, when the population action takes place. These properties should never be used together with the autoPopulate() method.

In addition, this class supports the common Object methods equals(Object), hashCode() and toString(). They use the primary keys of the table to calculate the hash code and the equality, while the toString() method prints the primary keys and their values together with the class name.

An example of a class that extends this class is:

  public class MyTable extends AutoTable {
      private Integer id = null;
      private String name = null;
      ...

      public Integer getId() {
          return id;
      }

      public void setId(Integer id) {
          this.id = id;
      }

      public String getName() {
          autoPopulate();
          return name;
      }

      public void setName(String name) {
          autoPopulate();
          this.name = name;
      }
      ...
 }
 
Note that the autoPopulate() method should be called before accessing the fields itself. This method is similar to what JPA does with the lazy loading of an object. The main difference is, this method needs to be defined explicitly by the developer, instead of adding arbitrary code to pojo classes.
  • Constructor Details

    • AutoTable

      public AutoTable()
  • Method Details

    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • autoPopulate

      protected void autoPopulate()
      Automatically populates the fields of this object. The ID field should already have been set.
    • markPopulated

      protected void markPopulated()
      Marks this object as already populated, so no further population needs to be done.