Creating models

To define a database table we need to add @Table annotation to a model class. You do not need to inherit from the base class or define any interface:

@Table(name = "Notes", database = AppDatabase.class)
public class Note {

    @PrimaryKey
    private Long id;

    ...

}

The database attribute is mandatory.This specifies in which database will be stored the table. Also, we can to specify the table name through attribute. If name not specified, the table name will be the same as the class name.

ReActiveAndroid uses the standard-constructor of your class to instantiate objects. If you define your own constructors you have to define a parameterless constructor as well.

Next we need to create primary key field. The field type must be long or Long. This field must be annotated with @PrimaryKey annotation:

@Table(name = "Notes", database = AppDatabase.class)
public class Note {

    @PrimaryKey
    private Long id;

    ...

}

For new object ID equals to zeo (or null, if the field type is Long). After saving object would be given a new identifier.

By default the primary key column name equals "id", but you can change it:

@Table(name = "Notes", database = AppDatabase.class)
public class Note {

    @PrimaryKey(name = "_id")
    private Long id;

    ...

}

Annotate all fields, which must be added to the table with @Column annotation. Thename attribute is optional, but if you not specified it, a column will be the same as a field name. The fields visibility modifier must be public or private only.

@Table(name = "Notes", database = AppDatabase.class)
public class Note {

    @PrimaryKey
    private Long id;
    @Column
    private String title;
    @Column
    private String text;

    ...

}

Also we can set constraints for the column, such as NOT NULL and the length of the column:

@Column(name = "title", notNull = true, length = 125)
private String title;

ReActiveAndroid supports work with all primitive types of Java and their wrappers. If you want to create a field with a custom type, do not forget to create a TypeSerializer for your data type.

To search for models ReActiveAndroid scans all classes. This can slow down the application, if you have many classes in your project. To speed up the work, you can add models directly through the configuration of the database:

DatabaseConfig appDatabaseConfig = new DatabaseConfig.Builder(AppDatabase.class)
    .addModelClasses(Note.class, Folder.class, ...)
    .build();

Unique Columns

A unique constraint prohibits multiple rows from having the same value in the same column or combination of columns, but allows some values to be null.

To define an unique constraint just annotate the required field with annotation @Column:

@Unique
@Column
private String title;

If want to define a composite unique key, create @IndexGroup annotation in uniqueGroups:

@Table(database = AppDatabase.class, 
        uniqueGroups = {@UniqueGroup(groupNumber = 1)}
public class Note {

    @Unique(unique = false, uniqueGroups = 1) 
    private String title;
    ...

}

The groupNumber is a unique identifier of group. We need to specify unique=false for any column used in a group so we expect the column to be part of a group. If true as well, the column will also alone be unique.

Setting indexes

Indexes are used to retrieve data from the database very fast. The users cannot see the indexes, they are just used to speed up searches/queries.

Note: Updating a table with indexes takes more time than updating a table without (because the indexes also need an update). So, only create indexes on columns that will be frequently searched against.

To create index add @IndexGroup to uniqueGroups

@Table(database = AppDatabase.class, 
        indexGroups = @IndexGroup(groupNumber = 1, name = "index_1"))
public class Note {

You can create unlimited number of index groups. The name value of @IndexGroup annotation used to define index table name. The name of this index will index_Note_index_1.

Then add @Index annotation for the corresponding table column:

@Index(indexGroups = 1)
@Column
private String title;

results matching ""

    No results matching ""