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.
 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 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.
 JdbcType jdbcType
          The JdbcType associated with the field.
 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.
 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:
""

jdbcType

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

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

defaultValue

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

Default:
""

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


Copyright © 2010. All Rights Reserved.