gpt4 book ai didi

android - Green DAO 关系

转载 作者:行者123 更新时间:2023-11-30 01:33:15 24 4
gpt4 key购买 nike

这是我的代码:

 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 表中的主键,是外键 UserActionReplies 表。

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

感谢任何帮助。

最佳答案

您需要使用.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有关关系的更多示例。

关于android - Green DAO 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35483546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com