com.j256.ormlite.field
Annotation Type DatabaseField


@Target(value=FIELD)
@Retention(value=RUNTIME)
public @interface DatabaseField

Annotation that identifies a field in a class that corresponds to a column in the database and will be persisted. Fields that are not to be persisted such as transient or other temporary fields probably should be ignored. For example:

 @DatabaseField(id = true)
 private String name;
 
 @DatabaseField(columnName = "passwd", canBeNull = false)
 private String password;
 

WARNING: If you add any extra fields here, you will need to add them to DatabaseFieldConfig as well.

Author:
graywatson

Optional Element Summary
 boolean canBeNull
          Whether the field can be assigned to null or have no value.
 String columnName
          The name of the column in the database.
 DataType dataType
          The DataType associated with the field.
 String defaultValue
          The default value of the field for creating the table.
 boolean foreign
          Field is a non-primitive object that corresponds to another class that is also stored in the database.
 boolean foreignAutoRefresh
          Set this to be true (default false) to have a foreign field automagically refreshed when an object is queried.
 String format
          Optional format information that can be used by various field types.
 boolean generatedId
          Whether the field is an auto-generated id field.
 String generatedIdSequence
          The name of the sequence number to be used to generate this value.
 boolean id
          Whether the field is the id field or not.
 boolean index
          Set this to be true (default false) to have the database add an index for this field.
 String indexName
          Set this to be a string (default none) to have the database add an index for this field with this name.
 boolean persisted
          Set this to be false (default true) to not store this field in the database.
 boolean throwIfNull
          If this is set to true (default false) then it will throw a SQLException if a null value is attempted to be de-persisted into a primitive.
 boolean unique
          Set this to be true (default false) to have the database insure that the column is unique to all rows in the table.
 boolean uniqueCombo
          Set this to be true (default false) to have the database insure that _all_ of the columns marked with this as true will together be unique.
 boolean uniqueIndex
          Set this to be true (default false) to have the database add a unique index for this field.
 String uniqueIndexName
          Set this to be a string (default none) to have the database add a unique index for this field with this name.
 String unknownEnumName
          If the field is an Enum and the database has a value that is not one of the names in the enum then this name will be used instead.
 boolean useGetSet
          Package should use get...() and set...() to access the field value instead of the default direct field access via reflection.
 int width
          Width of array fields (often for strings).
 

columnName

public abstract String columnName
The name of the column in the database. If not set then the name is taken from the field name.

Default:
""

dataType

public abstract DataType dataType
The DataType associated with the field. If not set then the Java class of the field is used to match with the appropriate DataType.

Default:
com.j256.ormlite.field.DataType.UNKNOWN

defaultValue

public abstract String defaultValue
The default value of the field for creating the table. Default is none.

Default:
"__ormlite__ no default value string was specified"

width

public abstract int width
Width of array fields (often for strings). Default is database-specific.

Default:
0

canBeNull

public abstract boolean canBeNull
Whether the field can be assigned to null or have no value. Default is true.

Default:
true

id

public abstract boolean id
Whether the field is the id field or not. Default is false. Only one field can have this set in a class. If you don't have it set then you won't be able to use the query, update, and delete by ID methods. Only one of this, generatedId(), and generatedIdSequence() can be specified.

Default:
false

generatedId

public abstract boolean generatedId
Whether the field is an auto-generated id field. Default is false. With databases for which DatabaseType.isIdSequenceNeeded() is true then this will cause the name of the sequence to be auto-generated. To specify the name of the sequence use generatedIdSequence(). Only one of this, id(), and generatedIdSequence() can be specified.

Default:
false

generatedIdSequence

public abstract String generatedIdSequence
The name of the sequence number to be used to generate this value. Default is none. This is only necessary for database for which DatabaseType.isIdSequenceNeeded() is true and you already have a defined sequence that you want to use. If you use generatedId() instead then the code will auto-generate a sequence name. Only one of this, id(), and generatedId() can be specified.

Default:
""

foreign

public abstract boolean foreign
Field is a non-primitive object that corresponds to another class that is also stored in the database. It must have an id field (either id(), generatedId(), or generatedIdSequence() which will be stored in this table. When an object is returned from a query call, any foreign objects will just have the id field set in it. To get all of the other fields you will have to do a refresh on the object using its own Dao.

Default:
false

useGetSet

public abstract boolean useGetSet
Package should use get...() and set...() to access the field value instead of the default direct field access via reflection. This may be necessary if the object you are storing has protections around it.

NOTE: The name of the get method must match getXxx() where Xxx is the name of the field with the first letter capitalized. The get must return a class which matches the field's. The set method must match setXxx(), have a single argument whose class matches the field's, and return void. For example:

 @DatabaseField
 private Integer orderCount;
 
 public Integer getOrderCount() {
        return orderCount;
 }
 
 public void setOrderCount(Integer orderCount) {
        this.orderCount = orderCount;
 }
 

Default:
false

unknownEnumName

public abstract String unknownEnumName
If the field is an Enum and the database has a value that is not one of the names in the enum then this name will be used instead. It must match one of the enum names. This is mainly useful when you are worried about backwards compatibility with older database rows or future compatibility if you have to roll back to older data definition.

Default:
""

throwIfNull

public abstract boolean throwIfNull
If this is set to true (default false) then it will throw a SQLException if a null value is attempted to be de-persisted into a primitive. This must only be used on a primitive field. If this is false then if the database field is null, the value of the primitive will be set to 0.

Default:
false

persisted

public abstract boolean persisted
Set this to be false (default true) to not store this field in the database. This is useful if you want to have the annotation on all of your fields but turn off the writing of some of them to the database.

Default:
true

format

public abstract String format
Optional format information that can be used by various field types. For example, if the Date is to be persisted as a string, this can set what format string to use for the date.

Default:
""

unique

public abstract boolean unique
Set this to be true (default false) to have the database insure that the column is unique to all rows in the table. Use this when you wan a field to be unique even if it is not the identify field. For example, if you have the firstName and lastName fields, both with unique=true and you have "Bob", "Smith" in the database, you cannot insert either "Bob", "Jones" or "Kevin", "Smith".

Default:
false

uniqueCombo

public abstract boolean uniqueCombo
Set this to be true (default false) to have the database insure that _all_ of the columns marked with this as true will together be unique. For example, if you have the firstName and lastName fields, both with unique=true and you have "Bob", "Smith" in the database, you cannot insert another "Bob", "Smith" but you can insert "Bob", "Jones" and "Kevin", "Smith".

Default:
false

index

public abstract boolean index
Set this to be true (default false) to have the database add an index for this field. This will create an index with the name columnName + "_idx". To specify a specific name of the index or to index multiple fields, use indexName().

Default:
false

uniqueIndex

public abstract boolean uniqueIndex
Set this to be true (default false) to have the database add a unique index for this field. This is the same as the index() field but this ensures that all of the values in the index are unique..

Default:
false

indexName

public abstract String indexName
Set this to be a string (default none) to have the database add an index for this field with this name. You do not need to specify the index() boolean as well. To index multiple fields together in one index, each of the fields should have the same indexName value.

Default:
""

uniqueIndexName

public abstract String uniqueIndexName
Set this to be a string (default none) to have the database add a unique index for this field with this name. This is the same as the indexName() field but this ensures that all of the values in the index are unique.

Default:
""

foreignAutoRefresh

public abstract boolean foreignAutoRefresh
Set this to be true (default false) to have a foreign field automagically refreshed when an object is queried. This will _not_ automagically create the foreign object but when the object is queried, a separate database call will be made to load of the fields of the foreign object via an internal DAO. The default is to just have the ID field in the object retrieved and for the caller to call refresh on the correct DAO.

NOTE: This will create another DAO object so that low memory devices may want to call refresh by hand.

Default:
false


Copyright © 2011. All Rights Reserved.