Previous Index Next

6 Generator Output and Tweaking

AuDAO generates the Java DAO sources using templates. We will show how to change the default behavior.

6.1 Config Element

All extra configurations are specified by the config element in the source XML file.

The optional config element can be defined on the global level (/database/config) or on the local table level (/database/tables/table/config). If both configs are specified for the given table, then the local config overrides the global one - but only for the config's subelements defined by the local config.

Example:

  <database xmlns="http://www.spoledge.com/audao" version="1.0">
    <config>
      <dto serial-version="-1"/>
      <dao-impl>
        <root>com.foo.MyRootDaoImpl</root>
      </dao-impl>
    </config>

    <tables>
      <table name="test_table">
        <config>
          <dto serial-version="-1234567890"/>
        </config>
        ...
      </table>
    </tables>
  </database>
The result test_table's config override's the dto subelement, but also inherits the global dao-impl subelement.

The only difference between the local and the global configuration is that subelement factory is allowed only in the global config (factory is only one class for all tables).

See also: XSD - global config, XSD - local config

6.2 DTO Classes

The generating of DTO classes can be influenced by the config/dto element in the source configuration XML file.

6.2.1 Serializing - serialVersionUID

If you serialize your DTO objects - into a stream or for example into a (web) session, then you can get error when retrieving old objects, but you already have installed a new versions of the DTO (e.g. with new fields).

To overcome these problems you can set the Serializable static field "serialVersionUID" explicitly:

  <config>
    <dto serial-version="-1"/>
  </config> 
This will generate the following line in the Java DTO code:
    public static final long serialVersionUID = -1L;
	

See also: XSD - config DTO

6.2.2 Methods equals() and hashCode()

Prior to AuDAO version 1.3 the DTO classes did not override the Object's methods equals(Object) and hashCode(). Now AuDAO is able to generate them and even more it is possible to choose among several possible implementations.

Syntax:

  <config>
    <dto equality="pk"/>
  </config> 
The allowed values are:
  • "full" - compares all column values and also "modified" flags. Use this if you want to compare/store into hash tables DTOs which do not have their DB peers.
  • "columns" - compares all column values (default). Use this if you want to compare/store into hash tables DTOs which are only partially filled.
  • "pk" - compares only primary key (and GAE parent keys if exist). Use this if you want to compare/store into hash tables DTOs which are inserted/fetched from datastore.
  • "identity" - same as if no equals nor hashCode methods were generated, but if parent exists, then the parent's methods are overriden. Use this if you want to be sure that the "native" implementation is used.
  • "none" - no equals nor hashCode methods are generated. If a parent exists, then the parent's methods are used. Use this if you do not use equals nor hashCode methods at all. Also use this for compatibility with AuDAO 1.2 behavior.

The equality attribute influences implementation of both equals and hashCode methods. It is not possible to choose the algorithms independently.

The implementation of the hashCode method can be influenced also by the gwt-compatible attribute's value.

See also: XSD - config DTO

6.2.3 Enum Column

Sometimes you may need to make "reflection" of the table's column. For example if you want to call JDBC statements directly, but without losing control on the column names you are using.

AuDAO allows you to generate a Java enumeration within the DTO class which describes all the column in the table. This is achieved by enabling the enum-column option as follows:

  <config>
    <dto enum-column="true"/>
  </config> 
which is generated into (Entity has only two columns "entity_id" and "entity_name"):
/**
 * This is a DTO class.
 *
 * @author generated
 */
public class Entity extends AbstractDto {

    public enum Column {
        ENTITY_ID( "entity_id" ),
        ENTITY_NAME( "entity_name" );

        private final String columnName;

        Column( String columnName ) {
            this.columnName = columnName;
        }

        public String columnName() {
            return columnName;
        }

        @Override
        public String toString() {
            return columnName;
        }
    };
		...  

Now you can access the column names from Java and compiler will check that the column names still exist.

See also: XSD - config/dto

6.2.4 Compatible DTOs with GWT (Google Web Toolkit)

If you want to generate DTO compatible with GWT (Java DTOs that can be converted into JavaScript by GWT without problems), then you should enable the gwt-compatible flag.

Syntax:

  <config>
    <dto gwt-compatible="true"/>
  </config> 

Currently these differences are applied:

  • method hashCode(): if the hash code is computed using a double column, then instead of using Float.floatToIntBits(column) which is not GWT compatible, the generator will just produce (int)column.

See also: XSD - config DTO

6.2.5 DTO Root Class

If you need to specify a different root parent class than the default one (AbstractDto (API)), then you can specify it also in config:

  <config>
    <dto>
      <root>com.foo.MyRootDto</root>
    </dto>
  </config> 

Your parent class should still extend the original AbstractDto (API).

You must provide a correct classpath including your custom DTO root class when compiling classes.

See also: XSD - config/dto

6.3 DAO Classes

The generating of DAO classes can be influenced by the config/dao element in the source configuration XML file.

6.3.1 Find method return type - array or list

The find methods return arrays by default. You can change it to lists (java.util.List) by the attribute find-many-result.

The following definition:

  <config>
    <dao find-many-result="list"/>
  </config> 
will generate the following Java DAO method for finding all records:
    public List<MyEntity> findAll(); 

See also: XSD - config/dao

6.3.2 Method getOrderExpr

For sorting in Oracle it is handy to have a method which will return the ORDER BY expression according to column type. If you define string columns for international charactes (i18n is true), then such methods will return Oracle specific sorting function for that column.

The following definition:

  <config>
    <dao method-get-order-expr="true"/>
  </config> 
will generate the following Java DAO:
    /**
     * Returns the expression used for ORDER BY clause.
     * @return the order expression for the given column, for example:
     *           company_name (MySQL)
     *           NLSSORT(company_name, 'NLS_SORT=generic_m') (Oracle)
     */
    public String getOrderExpr( Entity.Column col ); 

See also: XSD - config/dao

6.3.3 DAO Root Class

If you need to specify a different root parent class than the default one (AbstractDao (API)), then you can specify it also in config:

  <config>
    <dao>
      <root>com.foo.MyRootDao</root>
    </dao>
  </config> 

You must provide a correct classpath including your custom DTO root class when compiling classes.

See also: XSD - config/dao

6.4 DAO Implementation Classes

The generating of DAO classes can be influenced by the config/dao-impl element in the source configuration XML file.

6.4.1 DAO Impl Root Class

If you need to specify a different root parent class than the default one (AbstractDaoImpl (API), GaeAbstractDaoImpl (API) or GaeJdoAbstractDaoImpl (API)), then you can specify it also in config:

  <config>
    <dao-impl>
      <root>com.foo.MyRootDaoImpl</root>
    </dao-impl>
  </config> 

To avoid mismatch configurations for multiple targets, you may specify the target database type:

  <config>
    <dao-impl>
      <root dbtype="oracle">com.foo.MyOracleRootDaoImpl</root>
      <root dbtype="mysql">com.foo.MySQLRootDaoImpl</root>
      <root>com.foo.MyDefaultRootDaoImpl</root>
    </dao-impl>
  </config> 

Your parent class should still extend the original root parent !

You must provide a correct classpath including your custom DTO root class when compiling classes.

See also: XSD - config/dao-impl

6.4.2 Default DTO cache

The caching mechanism is currently implemented only for Google App Engine target dbtype (as of AuDAO version 1.5).

You can configure the default common cache which will be used by these operations (methods):

The cache is implemented as a static field, so different DAO instances share the same cache. The cache is also synchronized, so you can use multiple DAO instances in different threads without fear.

The mandatory attribute max-size specifies how many DTO objects can be cached at most:

  <config>
    <dao-impl>
      <default-cache max-size="50"/>
    </dao-impl>
  </config> 

The optional attribute expire-millis specifies how long the DTO can be cached - if the attribute is not specified, then the item can be cached forever:

  <config>
    <dao-impl>
      <default-cache max-size="50" expire-millis="60000"/>
    </dao-impl>
  </config> 

The attributes max-size and expire-millis are used for L1 - memory - caches. The implementation for Google App Engine uses combined L1+L2 caches - memory + MemcacheService using ChainedDtoCache (API).

See also: XSD - default-cache, GAE - Caches

Previous Index Next
Free Online Tool

Try our free online DAO generator.

Upload the configuration XML file and get the generated SQL scripts and Java classes for free !

Free GQL Parser

Download source or binary of our open-source GQL parser.

Query q =
 new GqlDynamic()
  .parseQuery(
   "SELECT * FROM Ent");
All GQL features are supported. More...