Android使用DBFlow创建简单的OneToMany关系 [英] Android create simple OneToMany Relation with DBFlow

查看:458
本文介绍了Android使用DBFlow创建简单的OneToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have simple two models and i'm trying to create simple OneToMany between them, as far as i'm newbie to use this library i can't use library documentation, my main model is:

@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中的

是未知的,并且出现此错误:

in ModelChannelPosts_Table file model.channel_video_containers is unknown and i get this error:

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

我如何为他们实现OneToMany?(使关系在ModelChannelPosts上的channel_video_containersModelVideos上的channelId之间建立关系?

how can i implementing OneToMany for them?( make relation ship between channel_video_containers on ModelChannelPosts and channelId on ModelVideos ?

推荐答案

这可能会晚一些,但对其他开发人员会有所帮助. 一对多关系将是这样

This might be late but it would be helpfull for other developers. One to Many Relation will be like this

@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() {
    }

    ...
}

这篇关于Android使用DBFlow创建简单的OneToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆