正确的方式来处理双向定向1:米绿DAO [英] Correct way to handle Bi directional 1:m in Green-DAO
问题描述
什么是一个双向定向1时务请插入一个新的对象变成绿色的DAO DB的正确方法:?m关系
what is the correct way to insert a new object into a Green-DAO DB when dealing with a Bi Directional 1:m relation?
可以说,我有一个谈话实体和消息实体的聊天应用程序。每次谈话都有消息的列表,每一个消息有一个父对话。
Lets say, I have a chat application that has a conversation entity and a message Entity. every conversation has a list of messages, and every message has a parent conversation.
我做什么截至目前是:
Conversation conv = new Conversation();
ConversationDao.insert(conv);
List<Message> list = conv.getMessageList();
Message msg = new Message();
MessageDao.insert(msg);
msg.setParent(conv.getId());
list.add(msg);
// SHOULD I UPDATE THE CONVERSATION IN THE DB???
在我看来,像我这样做并不是正确的,我很想得到一些指导广告正确的方式做到这一点。
it seems to me like I'm not doing this correctly and I would love to get some guidance ad to the correct way to do this.
在此先感谢...
编辑:的
这可以从我的评论中可以看出,试图实现code我写到这里的方式后,我收到了<$ 。C $ C>空而不是谈话,我试图链接
我改变了我的codeA位,现在它看起来是这样的:
as can be seen from my comment, after trying to implement code the way I wrote here, I get a null
instead of the conversation I was trying to link.
I changed my code a bit and now it looks like this:
private static void linkMessageToAuthorAndParent(Message messageObj, Thread parent) {
List<Message> threadsMessages = parent.getMessageList();
messageObj.setThread(parent);
messageDao.insert(messageObj);
threadsMessages.add(messageObj);
Log.d("DtabaseHelper.parseMessage", "message was inserted");
}
但同样,我的问题是,我不知道该连接现在是双向的。
我现在应该更新threadsDao?
but again, my problem is that I'm not sure the connection is now bi-directional.
should I now update the threadsDao?
推荐答案
尝试这种方式:
Conversation conv = new Conversation();
ConversationDao.insert(conv);
List<Message> list = conv.getMessageList();
Message msg = new Message();
msg.setParent(conv.getId()); // Set FK *before* inserting
MessageDao.insert(msg);
list.add(msg);
在href=\"http://greendao-orm.com/documentation/relations/\" rel=\"nofollow\">官方文档上的关系的是有部分解决和更新到许多
In the official documentation on relations there is a section "Resolving and Updating To-Many Relations" giving some background info on this.
这篇关于正确的方式来处理双向定向1:米绿DAO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!