Indexes

Index

Indexes represent a search path to the data. An Index can represent the following: -

  • A Table Index.
  • A Finder Method.

An index can be used to optimize queries in the form of a database index, but it is also an expression of how the application wishes to find data. By using Indexes to also represent finder methods the Java sessions and homes can build findBy and countBy methods for each Index.

Properties Reader

Indexes can have one or more columns, and are created with the following syntax: -

<ClassName>.index.0=<LogicalIndexName>(propertyName [,propertyName ... ])

<ClassName>.uniqueindex.0=<LogicalIndexName>(propertyName [,propertyName ... ])

<ClassName>.pseudoindex.0=<LogicalIndexName>(propertyName [,propertyName ... ])

<ClassName>.pseudouniqueindex.0=<LogicalIndexName>(propertyName [,propertyName ... ])

The logical index name is the name used in the properties file to refer to the index.

Where the propertyNames must be properties in the class. Pseudo indexes are not created in the SQL File or the database. The propertyName will refer to the property not the column name - thus a key is refered to by its property not the key name.

For example the code below creates 2 indexes, with the following findByMethods. JGenerator knows the types of the columns. The SQL index that is created is the <IndexName>, however by default the find by method names are synthetically generated.

UserProfile.index.0=Age(age)
UserProfile.index.1=Info(sex,age)

UserProfileSession.findByAge( int age ) throws FinderException, ValidationException
UserProfileSession.findBySexAndAge(sex,age) throws FinderException, ValidationException

Jdbc Reader

The Jdbc Reader can read index information in through the Jdbc Driver.

Reading indexes can be controlled by the Jdbc driver. They can be changed with the following properties, which are not set by default: -

jdbc.indexCatalog=<catalog)
jdbc.indexSchemaPattern=<pattern>

Index Names

Indexes have 2 names: -

  • Index Name
  • FindBy Name

The Index name is the SQL index name. The FindByName is the Session/Home method name. Where Indexes are automatically added the index name is generated (except for the primary key). The findByName is also generated, for (non-pseudo indexes) from the column names, and expressions. By default psuedo indexes long name is the logical name.

The findBy name can be overridden using the following syntax: -

<ClassName>.<LogicalIndexName>.findByName=<name>

For example: -

MyTable.XYIdex(x,y)
MyTable.XAndY.findByName=SillyName

Will create the following: -

MyTable.findBySillyName() throws FinderException, ValidationException

To create a findBy custom method that returns me the first X rows in a
certain order (useful for finding 1st, or top 10)

GameScore.pseudoindex.0=topScore(game)
GameScore.topScore.sortOrder=highScore DESC
GameScore.topScore.maxRowsMethod=true

now the method findByTopScore(gamePK, rowCount)

The indexName name can be overridden using the following syntax: -

<ClassName>.<LogicalIndexName>.indexName=<sql name>

For example: -

MyTable.Index(x,y)
MyTable.XAndY.indexName=_Index

Will create the following: -

CREATE INDEX _Index(x,y) On ...

 

Automatic Indexing

It is often useful to create indexes for primary keys and foreign keys. Indexes can be automatically created for keys for specific tables by setting one of the following respectively: -

<ClassName>.index=key
<ClassName>.index=pkey
<ClassName>.index=fkey

Automatically created Indexed are not pseudo indexes by default. To set an index to be a pseudo index the automatic indexes have following syntax

<ClassName>_<propertyName>.pseudo=true

For example an person foreign key on an Account table would have the following syntax: -

Account_person.pseudo=true

Index Methods

Tables have a number of accessor methods to get Indexes: -

  • public void addIndex( Index index );
  • public void removeIndex( Index index );
  • public int getIndexCount();
  • public Index getIndex( int indexNo );
  • public int getIndexOfIndex( Index index );
  • public Index getIndex( String indexName );
  • public Index getIndex( Column column );
  • public Index getLastIndex();
  • public Enumeration getIndexes();
  • public Index getPKIndex();