Android create simple OneToMany Relation with DBFlow

橙三吉。 提交于 2019-12-12 04:29:12

问题


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

    ...
}

and that has many ModelVideos, then ModelVideos belongs to 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() {
    }

    ...
}

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

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

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


回答1:


Don't know which version of DBFlow you are using but adding @ModelContainer annotation to ModelChannelPosts might help you




回答2:


Can you please check again by remove '@Column' from following code segment:

@Column
private List<ModelVideos> channel_video_containers;

Note: You might see an error of no setter for channel_video_containers so create one.

And put a foreign key in ModelVideos since many ModelVideos can belong to single ModelChannelPosts. e.g

In ModelVideos class

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

Rest looks fine. I am keeping in my mind the version of DBFlow = 4.0.0-beta3 (which is latest as per now)




回答3:


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

    ...
}

and ModelVideos class will be like

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

    ...
}


来源:https://stackoverflow.com/questions/41027698/android-create-simple-onetomany-relation-with-dbflow

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!