Hello Im new in GreenDao Im really like it but, Im trying to setup a messages plataform using django REST API and Retrfit with GreenDao Entities and I have this JSON message coming from the server:
{
id: 1
created_at: "2015-10-06T02:45:48Z"
subject: "First message"
message: "Here is our first message across the messaging system! I feel just like Samuel Morse."
read: true
sender: {
url: "https://app.herokuapp.com/api/users/461"
username: "travis"
email: ""
is_staff: false
}
receiver: 81
}
The problem is how can I put inside the message object in green dao "sender" as User Object not String or long an object inside Message object:
Schema schema = new Schema(DB_VERSION, ANDROID_APP_ID);
Entity privateMessage = schema.addEntity("PrivateMessage");
PropertyType user = schema.addEntity("User");
Property sender = privateMessage.addProperty(user., propertyName)
privateMessage.addStringProperty("id").primaryKey().unique();
privateMessage.addBooleanProperty("read");
privateMessage.addStringProperty("message");
user.addStringProperty("urlId").primaryKey().unique();
user.addStringProperty("url").unique();
user.addStringProperty("first_name");
user.addStringProperty("last_name");
user.addStringProperty("username").unique();
user.addStringProperty("email").unique();
user.addStringProperty("address");
user.addStringProperty("phone_number");
user.addStringProperty("password");
user.addStringProperty("group");
user.addBooleanProperty("sex");
user.addBooleanProperty("is_stuff");
user.addBooleanProperty("is_active");
user.addStringProperty("token");
user.addStringProperty("profile_picture");
user.addStringProperty("description");
user.addDateProperty("dob");
Property messagesThreatsId = privateMessage.addStringProperty("receiver").getProperty();
ToMany userToMessages = user.addToMany(privateMessage, messagesThreatsId);
Property conversations = privateMessage.addStringProperty("subject").getProperty();
Property created_at = privateMessage.addDateProperty("created_at").getProperty();
userToMessages.setName("LastMessages");
userToMessages.orderDesc(created_at);
You could use a one-to-many relation from user to message or a one-to-one from message to user, depending on which way do you want your relation working.
In the first case, you could get for each user all the messages send by this user. In the second, you could get for each message the user that send it. You always could do both
Keep in mind that, if the JSON have this format, you should first find the user id using the url field and after put it in the relation.
Here you have all the info regarding relations.