as you can probably remember from my previous question about bidirectional relations in Green DAO, I have a chat that consists of conversations and messages.
every message has a parent conversation, and the conversation has a list of messages.
when I try and add a new message, it works perfectly as long as I don't close the application.
This is the code I use for adding a message to an existing conversation:
I now have my code that does this:
Conversation conv = getConversation();
List<Message> list = conv.getMessageList();
Message msg = new Message();
msg.setConversationId(conv,getId());
MessageDao.insert(msg);
list.add(msg);
conv.update();
// ConversationDao.update(conv);
when I open the application the next time and call conv.getMessageList()
, the result is an empty ArrayList
.
what am I doing wrong?
EDIT:
I changed my code and now it looks like this:
Conversation conv= getCOnversation();
Message msg = new Message();
msg.setConversation(conv);
MessageDao.insert(msg);
conv.resetMessageList();
unfortunately, now more than ever (even on same run), still calling conv.getMessageList()
returns an empty java.list
.
EDIT 2:
here is code for the generator (this is not the real code, but only what is important to the question.
Entity message = schema.addEntity("Message");
message.addIdProperty().autoincrement();
Entity conversation =schema.addEntity("Conversation");
conversation.addIdProperty().autoincrement();
Property parentConversation = message.addLongProperty("parentConversation")
.getProperty();
Property messages = conversation.addLongProperty("messages").getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);
EDIT 3
for now, instead of calling conv.getMessageList();
I running this line and it work fine.
List<Message> messageList = MessageDao.queryBuilder()
.where(Properties.parentConversation.eq(conv.getId())
.list()
I suspect a major issue for me or greenrobot in the implementation of all this.
is this an issue of running the above code while Session.runInTx
?
This is the problematic section:
Property parentConversation = message.addLongProperty("parent")
.getProperty();
Property messages = conversation.addLongProperty("messages")
.getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);
For 1:N relations, you must use the same property for both your ToOne and ToMany relation. Try this and regenerate your code:
Property parentConversation = message.addLongProperty("parent")
.getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, parentConversation);