title: Automatically updating primary key in newly inserted Room Entity
body: <p>When you add a freshly created object in Room, with auto generation of the
  primary key, the newly created object keeps it’s undefined primary key. This
  proves inconvenient when you need to modify this object and persist the
  changes.</p>
  
  <p>Let’s take an example.</p>
  
  <h2>Entity Dao</h2>
  
  <p>So, let’s create a <code>User</code> whose <code>id</code> is auto-generated.</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity()
  public class User {
      @PrimaryKey(autoGenerate = true)
      @ColumnInfo(name = "id")
      private int id;
  
      @ColumnInfo(name = "email")
      private String email;
  
      public User(String email) {
          this.email = email;
      }
  
      // Other methods, especially getter and setter for each field
  
      // …
  }
  </code></pre>
  
  <p>We have the following Dao:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class UserDao {
      @Insert
      void insert(User user);
  
      @Update
      int update(User user);
  
      // …
  }
  </code></pre>
  
  <p>We interract through the following Repository:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Repository {
      private UserDao userDao;
  
      public void addUser(User user) {
          userDao.insert(user);
      }
  
      public void update(User user) {
          userDao.update(user);
      }
  
      // …
  }
  </code></pre>
  
  <h2>Inserting and updating an Entity</h2>
  
  <p>So, now if we do this, <code>user</code> will not be updated, because <code>User.id</code> will stay at 0 and <code>Update</code> annotation <a href="https://developer.android.com/reference/android/arch/persistence/room/Update" rel="nofollow noreferrer">updates the row only if the primary key already exists</a>.</p>
  
  <pre class="lang-java prettyprint-override"><code>Repository r = new Repository();
  User user = new User("me@my.email");
  r.addUser(user);
  user.setEmail("new@user.email");
  </code></pre>
  
  <h2>Proposed solution</h2>
  
  <p>To tackle this issue, we could change the <code>addUser</code> method of the <code>Repository</code> to update id after insertion.
  <a href="https://stackoverflow.com/questions/44364240/android-room-get-the-id-of-new-inserted-row-with-auto-generate">This question</a> gave me the following idea:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Repository {
      private UserDao userDao;
  
      public void addUser(User user) {
          long rowid = userDao.insert(user);
          user.setId(userDao.getIdFromRowid(rowid));
      }
  
      // …
  }
  
  </code></pre>
  
  <p>With this Dao (note that <code>insert</code> returns a <code>long</code> instead of <code>void</code>):</p>
  
  <pre class="lang-java prettyprint-override"><code>public class UserDao {
      @Insert
      long insert(User user);
  
      @Update
      int update(User user);
  
      @Query("SELECT id FROM user WHERE ROWID = :rowid")
      int getIdFromRowid(long rowid);
  
      // …
  }
  </code></pre>
  
  <p>Would this be an acceptable solution in the Android Jetpack paradigm? Do you
  see any pitfall?</p>