How to annotate default value inside Android class object?

I could not find any information on how to annotate the value SQL - DEFAULT, scanning documents @ColumnInfo for new Android Persistence Library.

Does Room provide annotation for default values?

My current solution would be to manually create the appropriate table ...

CREATE TABLE MyTable (
  ...
  MyDefaultValuedCol  TEXT DEFAULT 'Default Value',
  MyDefaultFlagCol    INT  DEFAULT 1
)

... and put the room on top.

@Entity(tableName = "MyTable")
class MyClass {
    ...

    public String MyDefaultValuedCol;

    public boolean MyDefaultFlagCol;

}
+16
source share
4 answers

There is no annotation for the default value in the room, but you can set the default value in your entity as follows:

@Entity(tableName = "MyTable")
class MyClass {
    ...

    public String MyDefaultValuedCol = "defaultString";

    public boolean MyDefaultFlagCol = true;

}
+9
source

Entity .

@Entity(tableName = "Dashboard")
public class Dashboard {
@PrimaryKey
@NonNull
@ColumnInfo(name = "claimNumber")
private String claimNumber;
private String percentage = "0";
private String imagePath = "";

@NonNull
public String getClaimNumber() {
    return claimNumber;
}

public void setClaimNumber(@NonNull String claimNumber) {
    this.claimNumber = claimNumber;
}



public String getPercentage() {
    if (percentage == null || percentage.isEmpty()) {
        return "0";
    }
    return percentage;
}

public void setPercentage(String percentage) {
    this.percentage = percentage;
}

public String getImagePath() {
    return imagePath;
}

public void setImagePath(String imagePath) {
    this.imagePath = imagePath;
}

public Dashboard(@NonNull String claimNumber,  String percentage, String imagePath) {
    this.claimNumber = claimNumber;

    this.percentage = percentage;
    this.imagePath = imagePath;
}

}

+3

, , "onDelete = CASCADE", , null, :

int parent1Id = 0;
int parent2Id = 0;  
//should be:
Long parent1Id = null;
Long parent2Id = null;

, , / , .

0

2.2.0 @ColumnInfo , .

@Entity(tableName = "users")
data class User(
    @PrimaryKey val id: Long,
    @ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
)
0
source

Source: https://habr.com/ru/post/1691107/


All Articles