Android GreenDao common notes

Posted by Azu on Wed, 11 Dec 2019 20:25:16 +0100

  1. @Entity -- Entity annotation
public @interface Entity {

    /**
     * The name of the table in the database, which defaults to the class name of the entity 
     */
    String nameInDb() default "";

    /**
     *  Define index, which can span multiple columns (the default is the number of entity class member variables) 
     */
    Index[] indexes() default {};

    /**
     * Tag create database table 
     * Set false if a table maps multiple entity classes or creates an out of table GreenDao 
     */
    boolean createInDb() default true;

    /**
     *  Tell GreenDao which schema the current entity belongs to 
     */
    String schema() default "default";

    /**
     *  Entity activity flag bit (default is false) 
     *  If set to true, the entity has update, delete and refresh methods 
     */
    boolean active() default false;
  }
  1. @NotNull -- the value of the current column in the setting table cannot be empty

  2. @Convert -- specify custom type (@ link PropertyConverter)

public @interface Convert {  
    /** Transformation class*/  
    Class<? extends PropertyConverter> converter();  
  
    /** 
     * Columns persisted in the database 
     * This is limited to classes supported by GreenDao 
     */  
    Class columnType();  
}  
  1. @Generated -- the constructor or method generated by greendao run. The code marked by this can be changed or cleared at the next run
public @interface Generated {  
    int hash() default -1;  
}
  1. @Id -- primary key Long type. Self growth can be set through @ Id(autoincrement = true). The field marked by this annotation must be Long, which means it is the primary key in the database, and it is self increasing by default.
public @interface Id {  
    /** 
      * Set whether it is self growth, default to false 
      */  
    boolean autoincrement() default false;  
} 
  1. @Index -- use @ index as a property to create an index; define multi column index (@ link entity × indexes())
public @interface Index {  
    /** 
     * Create the property index of the table through comma interval, such as "propertyA, propertyB, propertyC" 
     * To specify sorting, add ASC (ascending) or desc (descending) after listing, for example, "propertyA DESC, propertyB ASC" 
     *  Can only be set if {@ link entity ා indexes()} is used in entity class 
     */  
    String value() default "";  
  
    /** 
     * Optional index of table 
     * Default to member variable in entity class 
     */  
    String name() default "";  
  
    /** 
     * Whether to set unique property for property. The default is false 
     */  
    boolean unique() default false;  
}  
  1. @JoinEntity -- define table join relationship
public @interface JoinEntity {  
    /** Added entity class */  
    Class<?> entity();  
  
    /** Column index of source table */  
    String sourceProperty();  
  
    /** The attribute of the source entity in the join table*/  
    String targetProperty();  
}
  1. @JoinProperty -- define name and reference name property relationship
public @interface JoinProperty {  
    /** The name in the entity corresponds to the name of the reference */  
    String name();  
  
    /** Name of reference */  
    String referencedName();  
}
  1. @Keep -- the annotated snippet remains unchanged the next time GreenDao runs
    1. Annotation entity class: this class cannot be modified by default
    2. Annotate other code segments. It is forbidden to modify the annotated code segments by default

  2. @OrderBy -- specify sort

public @interface OrderBy {  
    /** 
     * Create the property index of the table through comma interval, such as "propertyA, propertyB, propertyC" 
     * To specify sorting, add ASC (ascending) or desc (descending) after listing, for example, "propertyA DESC, propertyB ASC" 
     *  Sort ascending by default 
     *  If not set, sort by primary key by default 
     */  
    String value() default "";  
} 
  1. @Property -- set the column name corresponding to a non default relationship mapping. For example, the default field name is @ Property (nameInDb="name")
public @interface Property {  
    /** 
     * Use field name by default 
     */  
    String nameInDb() default "";  
}  
  1. @ToMany -- defines the relationship with multiple entity objects
public @interface ToMany {  
    /** 
     * Target entity holds the name of the source entity 
     * This must be set, otherwise {@ link JoinProperty} or {@link JoinEntity} is specified 
     */  
    String referencedJoinProperty() default "";  
  
    /** 
     * Index source table column - > target column 
     * This must be set, otherwise {@ link JoinProperty} or {@link JoinEntity} is specified 
     */  
    JoinProperty[] joinProperties() default {};  
} 
  1. @ToOne -- defines the relationship with another entity (an entity object)
public @interface ToOne {  
    /** 
     * Attribute name of related entity in the table 
     * If the parameter does not exist, the additional column automatically creates a save key 
     */  
    String joinProperty() default "";  
} 
  1. @Transient -- columns of the database table will not be generated after the secondary tag is added

  2. @Unique -- adds a unique constraint to database columns

Topics: Android Database Attribute