0

我有两个简单的模型,我正在尝试在它们之间创建简单的 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_containersonModelChannelPostschannelIdon之间建立关系ModelVideos

4

3 回答 3

0

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

于 2016-12-09T21:01:56.463 回答
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() {
    }

    ...
}
于 2017-10-11T08:28:37.677 回答
0

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

@Column
private List<ModelVideos> channel_video_containers;

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

并在 ModelVideos 中放置一个外键,因为许多 ModelVideos 可以属于单个 ModelChannelPosts。例如

在 ModelVideos 类中

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

休息看起来不错。我牢记 DBFlow = 4.0.0-beta3 的版本(目前是最新的)

于 2016-12-14T14:04:35.103 回答