如何在房间数据库中存储多个模型?

时间:2019-06-03 15:19:49

标签: java android database model android-room

我想将我的项目模型对象存储在Room数据库中,仅在此模型中使用其他模型。我的错误是我必须考虑使用类型转换器。我对外键有更多的想法,有人可以帮我吗?

@Entity(tableName = "items_table")
public class Item implements Parcelable {

    @PrimaryKey(autoGenerate = true)
    private int id;

    @SerializedName("images")
    private ImageTrans imageTrans;

    @SerializedName("ratings")
    private Ratings ratings;


    @ColumnInfo(name = "item")
    @SerializedName("name")
    private String name;

    @SerializedName("description")
    private String description;

    @SerializedName("cost")
    private int cost;

    @SerializedName("type")
    private String type;

    @SerializedName("rarity")
    private String rarity;

    public Item() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public ImageTrans getImageTrans() {
        return imageTrans;
    }

    public int getCost() {
        return cost;
    }

    public String getType() {
        return type;
    }

    public String getRarity() {
        return rarity;
    }

    public Ratings getRatings() {
        return ratings;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setImageTrans(ImageTrans imageTrans) {
        this.imageTrans = imageTrans;
    }

    public void setCost(int cost) {
        this.cost = cost;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setRarity(String rarity) {
        this.rarity = rarity;
    }

    public void setRatings(Ratings ratings) {
        this.ratings = ratings;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(description);
        dest.writeInt(cost);
        dest.writeString(type);
        dest.writeString(rarity);
        dest.writeParcelable(ratings, flags);
    }

    protected Item(Parcel in) {
        name = in.readString();
        description = in.readString();
        cost = in.readInt();
        type = in.readString();
        rarity = in.readString();
        ratings = in.readParcelable(Ratings.class.getClassLoader());
    }

    public static final Creator<Item> CREATOR = new Creator<Item>() {
        @Override
        public Item createFromParcel(Parcel in) {
            return new Item(in);
        }

        @Override
        public Item[] newArray(int size) {
            return new Item[size];
        }
    };
}




public class Ratings implements Parcelable {

    @SerializedName("avgStars")
    private Double avgStars;

    public Double getAvgStars() {
        return avgStars;
    }

    public void setAvgStars(Double avgStars) {
        this.avgStars = avgStars;
    }

    protected Ratings(Parcel in) {
        if (in.readByte() == 0) {
            avgStars = null;
        } else {
            avgStars = in.readDouble();
        }
    }

    public static final Creator<Ratings> CREATOR = new Creator<Ratings>() {
        @Override
        public Ratings createFromParcel(Parcel in) {
            return new Ratings(in);
        }

        @Override
        public Ratings[] newArray(int size) {
            return new Ratings[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (avgStars == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeDouble(avgStars);
        }
    }
}

0 个答案:

没有答案