How to annotate a default value inside a android room entity?

前端 未结 5 857
故里飘歌
故里飘歌 2020-12-09 09:22

I couldn\'t find any information how to annotate a SQL - \"DEFAULT\" value while looking into the @ColumnInfo docs for the new Android Persistence Library.

Does Room

相关标签:
5条回答
  • 2020-12-09 10:04

    For any one facing the situation when you have two foreign keys and "onDelete = CASCADE" you can set the foreign key to a data type that can be set to null for example:

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

    that way the DB knows that a particular Object/row has no other parent of different type when attempting to delete it when its parent is deleted.

    0 讨论(0)
  • 2020-12-09 10:13

    You can check inside your getter method of Entity and set some default value there.

    @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;
    }
    

    }

    0 讨论(0)
  • 2020-12-09 10:15

    Room hasn't any annotation for default value, but you can set default value in your entity like this:

    @Entity(tableName = "MyTable")
    class MyClass {
        ...
    
        public String MyDefaultValuedCol = "defaultString";
    
        public boolean MyDefaultFlagCol = true;
    
    }
    
    0 讨论(0)
  • 2020-12-09 10:18

    You can set the default value using @ColumnInfo annotation-

    @ColumnInfo(defaultValue = "No name")
    public String name;
    

    and

    @ColumnInfo(defaultValue = "0")
    public int flag;
    

    or for any kind of data types check the reference from here Google developer doc

    0 讨论(0)
  • 2020-12-09 10:26

    With the release of room persistence 2.2.0, there's a new property added to @ColumnInfo annotation which can be used to specify the default value of a column. See documentation.

    @Entity(tableName = "users")
    data class User(
        @PrimaryKey val id: Long,
        @ColumnInfo(name = "user_name", defaultValue = "temp") val name: String
        @ColumnInfo(name = "last_modified", defaultValue = "CURRENT_TIMESTAMP" ) val lastModified: String
    )
    
    0 讨论(0)
提交回复
热议问题