Package io.ran

Annotation Interface Key


@Key is used to mark a field as a key in the database, for indexing and ordering purposes.

A key can consist of multiple fields, if they all have a Key annotation with the same name.

A field can be a member of multiple keys, if it has a Key annotation (with a different name) for each key it is a member of.

By default, ordering of the keys follows the order of the fields in the model class, but this can be overridden by setting the order value.

The name of the key in the database is set by the name value.

Example:


 public class User {
    @Key(order = 1, name = "user_idx")
 	public Long id;
    @Key(order = 2, name = "user_idx")
 	public String name;
 	public String email;
 }
 

This will expect a key named "user_idx" with the fields "id" and "name" in that order.

The following SQL would be the code needed to create the key:


 CREATE INDEX user_idx ON user (id, name);
 
See Also:
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    The name of the key in the database used for this field.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    If not set, the order of the fields on the model class will be used for ordering.
    boolean
    If true, values for this key must be unique, as a primary key would.
  • Element Details

    • name

      String name
      The name of the key in the database used for this field. Used to group parts of the same key.
      Returns:
      the name of the key, should only contain alphanumerics and underscores only
    • order

      int order
      If not set, the order of the fields on the model class will be used for ordering.
      Returns:
      the order in the keyset, must be unique
      Default:
      -1
    • unique

      boolean unique
      If true, values for this key must be unique, as a primary key would.

      Composite key is unique if ANY of the fields it contains are marked as such.

      Null values are ALWAYS considered to be unique, e.g. it's possible to have multiple rows with null values for a unique key.

      Warning: If you are using MSSQL and are creating the indices yourself, this behavior may be different, unless a WHERE clause is used to filter out null values. If you are using Valqueries to generate the key, it will contain this clause to keep the behavior consistent across different database providers.

      Returns:
      true if the key should be unique
      Default:
      false