title: How to do a DB reverse lookup with Akka Persistence in Lagom?
body: <p>I'm building a token authentication microservice using <code>Lagom</code>, and have run into a wrinkle.  My data model is currently</p>
  
  <pre><code>table Token (
    groupName varchar(255) PK,
    token varchar(255) (logical UUID type)
  )
  </code></pre>
  
  <p>I have three methods, <code>createToken</code>, <code>getToken</code>, and <code>validateToken</code>.  The first two take a group name, which maps nicely to the entity id as laid out in <a href="https://www.lagomframework.com/documentation/current/scala/PersistentEntity.html#Persistent-Entity" rel="nofollow noreferrer">https://www.lagomframework.com/documentation/current/scala/PersistentEntity.html#Persistent-Entity</a>.<br>
  The third takes a token in the body of the request, and then returns a json payload with the <code>groupName</code> and <code>token</code> if it succeeds.  </p>
  
  <p>Lagom abstracts the actual SQL, and you access the persistence layer by doing something like this: <code>val ref = persistentEntityRegistry.refFor[DptokenEntity](groupName)</code>.  The actual persistence code is this here:</p>
  
  <pre><code>class DptokenEntity extends PersistentEntity {
  
    override type Command = DptokenCommand[_]
    override type Event = DptokenEvent
    override type State = DptokenState
  
    override def initialState: DptokenState = DptokenState(None)
  
    override def behavior: Behavior = {
      case DptokenState(_) =&gt; Actions()
        .onCommand[PersistToken, TokenResponse] {
        case (PersistToken(token), ctx, state) =&gt;
          ctx.thenPersist(TokenPersisted(token)) { _ =&gt;
            ctx.reply(token)
          }
      }.onReadOnlyCommand[GetToken, TokenResponse] {
        case (GetToken(groupName), ctx, state) =&gt;
          state.token match {
            case Some(token) =&gt; ctx.reply(token)
            case None =&gt; ctx.invalidCommand(s"No token exists for $groupName")
          }
      }.onEvent {
        case (TokenPersisted(token), _) =&gt; DptokenState(Some(token))
      }
    }
  }
  
  case class DptokenState(token: Option[TokenResponse])
  </code></pre>
  
  <p>I am struggling to understand how to do the reverse look up with the above model due to the fact that if I lookup the <code>entity</code> by the token, I will ostensibly get a new row in the database where the relationship is <code>token</code> -> <code>groupName</code>, which then poses massive problems for updates, etc...</p>
  
  <p>I wanted to check with the good folks on SO before I just rip out the Event Sourcing stuff and use raw Slick.</p>