title: Many to Many relations with Room & LiveData
body: <p>I have a rest api that returns a list of places, which have a list of categories:</p>
  
  <pre><code>{
        "id": "35fds-45sdgk-fsd87",
        "name" : "My awesome place",
         "categories" : [
            {
              "id": "cat1",
              "name" : "Category 1"
            },
            {
              "id": "cat2",
              "name" : "Category 2"
            },
            {
              "id": "cat3",
              "name" : "Category 3"
            }
         ]
  }
  </code></pre>
  
  <p>So using retrofit I get these from the remote server with these model classes:</p>
  
  <pre><code>data class Category(var id: String, var name: String)
  
  data class Place(
    var id: String,
    var name: String,
    var categories: List&lt;Category&gt;
  )
  </code></pre>
  
  <p>Problem is -- I want the viewModel to always retrieve from a local Room Database returning Flowables and just trigger refresh actions that will update the database and thus the view.</p>
  
  <p>DAO method example:</p>
  
  <pre><code>@Query("select * from Places where placeId = :id")
  fun getPlace(id: String): Flowable&lt;Place&gt;
  </code></pre>
  
  <p>So I tried modeling those two classes like this:</p>
  
  <pre><code>@Entity
  data class Category(var id: String, var name: String)
  
  
  @Entity
  data class Place(
    @PrimaryKey
    var id: String,
    var name: String,
    var categories: List&lt;Category&gt;
  )
  </code></pre>
  
  <p>But of course Room is not able to process relations on its own. I have seen <a href="https://proandroiddev.com/android-room-handling-relations-using-livedata-2d892e40bd53" rel="noreferrer">this post</a> which just retrieves from the local database the previous list of cities, but this case doesnt match that one.  </p>
  
  <p>Only option I could think of is to save the categories in the database as a JSON string but this is losing the relational quality of the database...</p>
  
  <p>This seems like a pretty common use case but I haven't found much info about it.</p>