title: Spring Boot R2DBC with MySQL - Exception: Table not found
body: <p>I'm extremely new to <strong>String Boot</strong> and backend development (maybe three days or less) and I have the desire to build <strong>REST API</strong> to consume from different clients.</p>
  <p>So I started by a simple demo app that has an endpoint called <code>/register</code>. We post a <code>JSON</code> string with <code>username</code> and <code>password</code> to create a new user if not exist.</p>
  <p>I was using <code>JPA</code> with <code>HSQLDB</code> and it worked fine persisting on memory. But recently I wanted to use <code>RxJava</code> since I'm familiar with on Android, so I switched to <code>R2DBC</code> with <code>MySQL</code>.</p>
  <p><code>MySQL</code> server is running fine on port <code>3306</code> and the app was tested using PostMan on <code>localhost:8080</code></p>
  <p>The problem occurs when I try to query users table or insert entities and it looks like this:</p>
  <pre><code>{
      &quot;timestamp&quot;: &quot;2020-03-22T11:54:43.466+0000&quot;,
      &quot;status&quot;: 500,
      &quot;error&quot;: &quot;Internal Server Error&quot;,
      &quot;message&quot;: &quot;execute; bad SQL grammar [UPDATE user_entity SET username = $1, password = $2 WHERE user_entity.id = $3]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [42102] [42S02] Table \&quot;USER_ENTITY\&quot; not found; SQL statement:\nUPDATE user_entity SET username = $1, password = $2 WHERE user_entity.id = $3 [42102-200]&quot;,
      &quot;path&quot;: &quot;/register&quot;
  }
  </code></pre>
  <p>Here's the full <a href="https://docs.google.com/document/d/1q_042QlMpTfvPZCpUDW-dNRN4GgKT2yz_VdIufWYHCw/edit?usp=sharing" rel="nofollow noreferrer">logfile</a> for the exception.</p>
  <p>I Have been looking for a solution for hours and I seem like not finding it anywhere, so I hope that I will find it here.</p>
  <h1>Let's break down the project so it's easier to find  the solution:</h1>
  <p><strong>1. database:</strong></p>
  <p><a href="https://i.stack.imgur.com/k9WHw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/k9WHw.png" alt="enter image description here" /></a></p>
  <p><strong>2. application.properties:</strong></p>
  <pre><code>logging.level.org.springframework.data.r2dbc=DEBUG
  spring.datasource.url=jdbc:mysql://localhost:3306/demodb
  spring.datasource.username=root
  spring.datasource.password=root
  </code></pre>
  <p><strong>3. DatabaseConfiguration:</strong></p>
  <pre><code>@Configuration
  @EnableR2dbcRepositories
  class DatabaseConfiguration : AbstractR2dbcConfiguration() {
  
      override fun connectionFactory(): ConnectionFactory
           = ConnectionFactories.get(
                  builder().option(DRIVER, &quot;mysql&quot;)
                      .option(HOST, &quot;localhost&quot;)
                      .option(USER, &quot;root&quot;)
                      .option(PASSWORD, &quot;root&quot;) 
                      .option(DATABASE, &quot;demodb&quot;)
                      .build()
          )
  
  
  }
  </code></pre>
  <p><strong>4. RegistrationController:</strong></p>
  <pre><code>@RequestMapping(&quot;/register&quot;)
  @RestController
  class RegistrationController @Autowired constructor(private val userService: UserService) {
  
      @PostMapping
      fun login(@RequestBody registrationRequest: RegistrationRequest): Single&lt;ResponseEntity&lt;String&gt;&gt;
          = userService.userExists(registrationRequest.username)
              .flatMap { exists -&gt; handleUserExistance(exists, registrationRequest) }
      
      private fun handleUserExistance(exists: Boolean, registrationRequest: RegistrationRequest): Single&lt;ResponseEntity&lt;String&gt;&gt; 
          = if (exists) Single.just(ResponseEntity(&quot;Username already exists. Please try an other one&quot;, HttpStatus.CONFLICT))
              else userService.insert(User(registrationRequest.username, registrationRequest.password)).map { user -&gt;
                  ResponseEntity(&quot;User was successfully created with the id: ${user.id}&quot;, HttpStatus.CREATED)
              }
      
  }
  </code></pre>
  <p><strong>5. UserService:</strong></p>
  <pre><code>@Service
  class UserService @Autowired constructor(override val repository: IRxUserRepository) : RxSimpleService&lt;User, UserEntity&gt;(repository) {
  
      override val converter: EntityConverter&lt;User, UserEntity&gt; = UserEntity.Converter
  
      fun userExists(username: String): Single&lt;Boolean&gt;
          = repository.existsByUsername(username)
  
  }
  </code></pre>
  <p><strong>6. RxSimpleService:</strong></p>
  <pre><code>abstract class RxSimpleService&lt;T, E&gt;(protected open val repository: RxJava2CrudRepository&lt;E, Long&gt;)  {
  
      protected abstract val converter: EntityConverter&lt;T, E&gt;
  
      open fun insert(model: T): Single&lt;T&gt;
          = repository.save(converter.fromModel(model))
              .map(converter::toModel)
  
      open fun get(id: Long): Maybe&lt;T&gt;
          = repository.findById(id)
              .map(converter::toModel)
  
      open fun getAll(): Single&lt;ArrayList&lt;T&gt;&gt;
          = repository.findAll()
              .toList()
              .map(converter::toModels)
  
      open fun delete(model: T): Completable
          = repository.delete(converter.fromModel(model))
  
  }
  </code></pre>
  <p><strong>7. RxUserRepository:</strong></p>
  <pre><code>@Repository
  interface IRxUserRepository : RxJava2CrudRepository&lt;UserEntity, Long&gt; {
  
      @Query(&quot;SELECT CASE WHEN EXISTS ( SELECT * FROM ${UserEntity.TABLE_NAME} WHERE username = :username) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END&quot;)
      fun existsByUsername(username: String): Single&lt;Boolean&gt;
  
  }
  </code></pre>
  <p><strong>8. And finally, here's my UserEntity</strong></p>
  <pre><code>@Table(TABLE_NAME)
  data class UserEntity(
          @Id
          val id: Long,
          val username: String,
          val password: String
  )  {
  
          companion object {
              const val TABLE_NAME = &quot;user_entity&quot;
          }
  
          object Converter : EntityConverter&lt;User, UserEntity&gt; {
  
              override fun fromModel(model: User): UserEntity
                     = with(model) { UserEntity(id, username, password) }
  
              override fun toModel(entity: UserEntity): User
                     = with(entity) { User(id, username, password) }
  
          }
  }
  </code></pre>
  <p><code>User</code> and <code>RegistrationRequest</code> are just simple objects with username and password.
  What I have missed?
  Please leave a comment if you need more code.</p>