【问题标题】:Green DAO relationship绿色 DAO 关系
【发布时间】:2016-02-19 02:03:10
【问题描述】:

这是我的代码:

 Schema schema = new Schema(1, "com.core.greendao.db");

    /* Topic Model Table */
    Entity topic = schema.addEntity("Topic");
    topic.addLongProperty("topic_id").primaryKey();
    topic.addStringProperty("group_id").notNull();
    topic.addStringProperty("user_id");
    topic.addStringProperty("slug");
    topic.addStringProperty("message");
    topic.addStringProperty("reply_count");
    topic.addStringProperty("like_count");
    topic.addStringProperty("anon_status");
    topic.addStringProperty("link_data");
    topic.addStringProperty("created_at");
    topic.addStringProperty("locale");
    topic.addIntProperty("status");

    /* Reply Model Table */
    //TODO: Topic id add for relation
    Entity reply = schema.addEntity("Replies");
    reply.addLongProperty("reply_id").primaryKey();
    reply.addStringProperty("message");
    reply.addStringProperty("reply_count");
    reply.addStringProperty("like_count");
    reply.addStringProperty("anon_status");
    reply.addStringProperty("link_data");
    reply.addStringProperty("created_at");
    reply.addStringProperty("locale");
    reply.addIntProperty("status");

    /* User Model Table */
    //TODO: Topic id to add for relation
    Entity user = schema.addEntity("User");
    user.addIdProperty();
    user.addLongProperty("user_id");
    user.addStringProperty("url");
    user.addStringProperty("fullname");
    user.addStringProperty("tagline");
    user.addStringProperty("image");
    user.addStringProperty("category_title");

    /* Actions */
    //TODO: Topic id and Reply id for relation
    Entity actions = schema.addEntity("Actions");
    actions.addIdProperty();
    actions.addLongProperty("user_id");
    actions.addStringProperty("url");


    /*******************************************************************/

    Property topicIdForTopicUser = user.addLongProperty("topic_id").notNull().getProperty();
    user.addToOne(topic, topicIdForTopicUser);

    Property topicIdForTopicAction = actions.addLongProperty("topic_id").notNull().getProperty();
    actions.addToOne(topic, topicIdForTopicAction);

    Property topicIdForReply = reply.addLongProperty("topic_id").notNull().getProperty();
    reply.addToOne(topic, topicIdForReply);


    /*******************************************************************/

根据结构,topic_idTopic 表中的主键,是UserAction 中的外键Replies 表。

我从Topic 表中得到了正确的值。但是当我尝试根据topic_id从其他表中获取值时得到空点。

任何帮助表示赞赏。

【问题讨论】:

    标签: android greendao greendao-generator


    【解决方案1】:

    您需要使用.addToOne(table, property) 方法来指定关系。你也不需要为对象指定id,你可以使用.addIdProperty()例如

    Schema schema = new Schema(1, "com.core.greendao.db");
    Entity user = schema.addEntity("User");
    user.addIdProperty();
    
    Entity topic = schema.addEntity("Topic");
    topic.addIdProperty();
    Property userId = topic.addLongProperty("user_id").notNull().getProperty()
    topic.addToOne(user, userId);
    

    有关关系的更多示例,请参阅GreenDAO docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 2012-11-22
      • 2011-08-08
      • 1970-01-01
      • 2020-06-10
      相关资源
      最近更新 更多