Green DAO relationship

153 views Asked by At

This is my code :

 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);


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

As per the structure, topic_id is a primary key in the Topic table and is foreign key in the User, Action, and Replies tables.

I am getting proper values from the Topic table. But getting null point when I try to get values from other table on the basis of topic_id.

Any help appreciated.

1

There are 1 answers

0
Mike Porter On

You need to use .addToOne(table, property) method to specify the relationship. You also don't need to specify the id for the object, you can just use .addIdProperty() For example

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);

See GreenDAO docs for more examples of relationships.