@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
,
DatabaseFieldConfigLoader
, DatabaseFieldConfigLoaderTest, and DatabaseTableConfigUtil as well.
Modifier and Type | Fields and Description |
---|---|
static int |
DEFAULT_MAX_FOREIGN_AUTO_REFRESH_LEVEL
Default for the maxForeignAutoRefreshLevel.
|
static String |
DEFAULT_STRING
this special string is used as a .equals check to see if no default was specified
|
Modifier and Type | Optional Element and Description |
---|---|
boolean |
allowGeneratedIdInsert
If this is set to true then inserting an object with the ID field already set (i.e.
|
boolean |
canBeNull
Whether the field can be assigned to null or have no value.
|
String |
columnDefinition
Specify the SQL necessary to create this field in the database.
|
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 |
foreignAutoCreate
Set this to be true (default false) to have the foreign field will be automagically created using its internal
DAO if the ID field is not set (null or 0).
|
boolean |
foreignAutoRefresh
Set this to be true (default false) to have a foreign field automagically refreshed when an object is queried.
|
String |
foreignColumnName
Name of the foreign object's field that is tied to this table.
|
String |
format
Optional format information that can be used by various field types.
|
String |
fullColumnDefinition
Specify the SQL necessary to create this field in the database including the column name, which should be
properly escaped and in proper case depending on your database type.
|
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.
|
int |
maxForeignAutoRefreshLevel
Set this to be the number of times to refresh a foreign object's foreign object.
|
boolean |
persisted
Set this to be false (default true) to not store this field in the database.
|
Class<? extends DataPersister> |
persisterClass
Allows you to set a custom persister class to handle this field.
|
boolean |
readOnly
Set this to be true (default false) if this field is a read-only field.
|
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.
|
boolean |
version
Set this to be true (default false) to have this field to be a version field similar to
javax.persistence.Version.
|
int |
width
Width of array fields (often for strings).
|
public static final String DEFAULT_STRING
public static final int DEFAULT_MAX_FOREIGN_AUTO_REFRESH_LEVEL
maxForeignAutoRefreshLevel()
public abstract String columnName
public abstract DataType dataType
public abstract String defaultValue
NOTE: If the field has a null value then this value will be inserted in its place when you call you call
Dao.create(Object)
. This does not apply to primitive fields so you should just assign them in the class
instead.
public abstract int width
public abstract boolean canBeNull
public abstract boolean id
generatedId()
, and generatedIdSequence()
can be specified.public abstract boolean generatedId
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.public abstract String generatedIdSequence
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.public abstract boolean foreign
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.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; }
public abstract String unknownEnumName
public abstract boolean throwIfNull
public abstract boolean persisted
public abstract String format
public abstract boolean unique
public abstract boolean uniqueCombo
public abstract boolean index
indexName()
.public abstract boolean uniqueIndex
index()
field but this ensures that all of the values in the index are unique..public abstract String indexName
index()
boolean as well. To index multiple fields together in one index, each of
the fields should have the same indexName value.public abstract String uniqueIndexName
indexName()
field but this ensures that all of the values in the index are
unique.public abstract boolean foreignAutoRefresh
public abstract int maxForeignAutoRefreshLevel
NOTE: Increasing this value will result in more database transactions whenever you query for A, so use carefully.
public abstract Class<? extends DataPersister> persisterClass
DataPersister
public abstract boolean allowGeneratedIdInsert
generatedId()
is also true for the field.public abstract String columnDefinition
fullColumnDefinition()
.public abstract boolean foreignAutoCreate
Set this to be true (default false) to have the foreign field will be automagically created using its internal
DAO if the ID field is not set (null or 0). So when you call dao.create() on the parent object, any field with
this set to true will possibly be created via an internal DAO. By default you have to create the object using its
DAO directly. This only works if generatedId()
is also set to true.
Order order1 = new Order(); // account1 has not been created in the db yet and it's id == null order1.account = account1; // this will create order1 _and_ pass order1.account to the internal account dao.create(). orderDao.create(order1);
public abstract boolean version
public abstract String foreignColumnName
NOTE: Setting this implies foreignAutoRefresh()
is also set to true because there is no way to
refresh the object since the id field is not stored in the database. So when this is set, the field will be
automatically refreshed in another database query.
public abstract boolean readOnly
public abstract String fullColumnDefinition
columnDefinition()
should be used instead. This is used
if both this and columnDefinition()
are specified.This documentation is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.