Chirag
Chirag

Reputation: 967

GreenDao Foreign Keys set as null for ToMany relations by default

I am trying to model a to-many relation between Users and Articles using GreenDAO, where a User has many Articles.

Reference: GreenDAO Documentation on Relations

My GreenDAO's generator code is as follows:

 Schema schema = new Schema(1, "com.example.greendao.models");
 schema.enableKeepSectionsByDefault();
 schema.setDefaultJavaPackageDao("com.example.greendao.dao");

 Entity user = schema.addEntity("User");
 user.addIdProperty().primaryKey();
 user.addStringProperty("name");

 Entity article = schema.addEntity("Article");
 article.addIdProperty().primaryKey().autoincrement();
 article.addStringProperty("title");
 article.addStringProperty("content");
 Property userId = article.addLongProperty("userId").notNull().getProperty();

 ToMany userToArticles = user.addToMany(article, userId);
 userToArticles.setName("articles");

 new DaoGenerator().generateAll(schema, "app/src/main/java");

Next, I fetch the Users and Articles via Retrofitand convert it to List<User> using Gson


Sample HTTP API Response

[
  {
    "id": 1,
    "name": "Example Name 1",
    "articles": [
      {
        "id": 2,
        "title": "Example Title 2",
        "content": "Example Content 2"
      },
      {
        "id": 10,
        "title": "Example Title 10",
        "content": "Example Content 10"
      }
    ]
  },
  {
    "id": 2,
    "name": "Example Name 2",
    "articles": [
      {
        "id": 111,
        "title": "Example Title 111",
        "content": "Example Content 111"
      }
    ]
  }
]

After conversion, I get a List<User> object where the states of the first user are:


And Here is how I am inserting my Users and Articles

 daoSession.runInTx(new Runnable() {
     @Override
     public void run() {
         for (User user : users) {
             userDao.insertOrReplaceInTx(user);
             articleDao.insertOrReplaceInTx(user.articles());
         }
     }
 });


Problem:

When the user is fetched using UserDao, and the articles are retrieved using getArticles() method, the correct articles are retrieved but the userId field in article is null (which makes sense since userId is null already)

Is there a way that upon insertion of articles in ArticlesDAO, the value of userId could be automatically set to the id of the user instead of manually setting it to the userId everytime in code?

Note The foreign key constraints have been set already

Upvotes: 1

Views: 660

Answers (1)

Jofre Mateu
Jofre Mateu

Reputation: 2430

You should do something like this:

 daoSession.runInTx(new Runnable() {
     @Override
     public void run() {
         for (User user : users) {
             userDao.insertOrReplaceInTx(user);
             for (Article article : user.getArticles()) {
                 article.setUser(user);
                 articleDao.insertOrReplaceInTx(article);
             }
         }
     }
 });

I know it could be prettier but, AFAIK, you have to set the user for every entity one by one.

Upvotes: 1

Related Questions