Android使用DBFlow

时间:2016-12-07 21:28:41

标签: android dbflow

我有两个简单的模型,我试图在它们之间创建简单的OneToMany,只要我是新手使用这个库我不能使用库文档,我的主要模型是:

@Table(database = AppDatabase.class)
public class ModelChannelPosts extends BaseModel {
    @PrimaryKey
    private int id;

    @Column
    private String channelId;

    @Column
    private List<ModelVideos> channel_video_containers;

    @Column
    private String createdAt;

    @Column
    private String updatedAt;


    public ModelChannelPosts() {
    }

    @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "channel_video_containers")
    public List<ModelVideos> getVideos() {
        if (channel_video_containers == null || channel_video_containers.isEmpty()) {
            channel_video_containers = new Select()
                    .from(ModelVideos.class)
                    .where(ModelVideos_Table.channelId.eq(id))
                    .queryList();
        }
        return channel_video_containers;
    }

    ...
}

且有许多ModelVideos,然后ModelVideos属于ModelChannelPosts

@Table(database = AppDatabase.class)
public class ModelVideos extends BaseModel {

    @PrimaryKey
    private int                    id;

    @Column
    private String                 fileName;

    @Column
    private String                 videoLength;

    @Column
    private int                    channelId;

    public ModelVideos() {
    }

    ...
}
ModelChannelPosts_Table文件model.channel_video_containers中的

未知,我收到此错误:

Error:(168, 48) error: <identifier> expected

我如何为他们实施OneToMany?(在channel_video_containers上的ModelChannelPostschannelId上的ModelVideos之间建立关联?

3 个答案:

答案 0 :(得分:0)

不知道您使用的是哪个版本的DBFlow,但将@ModelContainer注释添加到ModelChannelPosts可能会对您有所帮助

答案 1 :(得分:0)

您可以通过从以下代码段中删除“@Column”来再次检查:

@Column
private List<ModelVideos> channel_video_containers;

注意:您可能会看到channel_video_containers没有setter的错误,因此请创建一个。

并在ModelVideos中放入一个外键,因为许多ModelVideos可以属于单个ModelChannelPosts。 e.g

在ModelVideos类

@Column
@ForeignKey(saveForeignKeyModel = false)
ModelChannelPosts modelChannelPosts;

休息看起来不错。我在脑海中记住DBFlow = 4.0.0-beta3的版本(现在是最新的)

答案 2 :(得分:0)

这可能会迟到,但对其他开发者来说会有所帮助。 一对多关系就像这样

@Table(database = AppDatabase.class)
public class ModelChannelPosts extends BaseModel {

    @PrimaryKey
    private int id;

    @Column
    private String channelId;

    private List<ModelVideos> channel_video_containers;

    @Column
    private String createdAt;

    @Column
    private String updatedAt;


    public ModelChannelPosts() {
    }

    @OneToMany(methods = {OneToMany.Method.ALL}, variableName = "channel_video_containers")
    public List<ModelVideos> getVideos() {
        if (channel_video_containers == null || channel_video_containers.isEmpty()) {
            channel_video_containers = new Select()
                    .from(ModelVideos.class)
                    .where(ModelVideos_Table.modelChannelPosts_id.eq(id))
                    .queryList();
        }
        return channel_video_containers;
    }

    ...
}

ModelVideos类就像

@Table(database = AppDatabase.class)
public class ModelVideos extends BaseModel {

    @PrimaryKey
    private int id;

    @Column
    private String fileName;

    @Column
    private String videoLength;

    @Column
    private int channelId;

    @ForeignKey(stubbedRelationship = true)
    ModelChannelPosts modelChannelPosts;

    public ModelVideos() {
    }

    ...
}