2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MyBatis There is no getter for property named xxx エラー

Posted at

Mybatisで以下のようなエラーが出た。

org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'dto' in 'class com.awba.manage.awbamanage.domain.UserRole'
	at org.apache.ibatis.reflection.Reflector.getGetInvoker(Reflector.java:373) ~[mybatis-3.5.3.jar:3.5.3]
	at org.apache.ibatis.reflection.MetaClass.getGetInvoker(MetaClass.java:163) ~[mybatis-3.5.3.jar:3.5.3]
	at org.apache.ibatis.reflection.wrapper.BeanWrapper.getBeanProperty(BeanWrapper.java:162) ~[mybatis-3.5.3.jar:3.5.3]

エラーが出ているクエリ

INSERT時にオブジェクトを引数に渡してそのフィールドをVALUEとしてセットするクエリ

UserRoleMapper.kt
    @Insert("""
        INSERT INTO user_role (
          user_id,
          role_id,
          role_name,
          deleted_at,
          created_at,
          updated_at)
        VALUES (
          #{dto.user_id},
          #{dto.role_id},
          #{dto.role_name},
          null,
          now(),
          now()
        )
    """)
    fun insertDefaultRoles(dto: UserRole)

原因

MyBatisはバインド変数#{xxx}使用時は、クラスのフィールド名を指定する必要がある。
上記だとUserRoleクラスのdtoというフィールドを探しにいってしまってエラーになっている。
dto.user_id → user_id でOK

正しい形

UserRoleMapper.kt
    @Insert("""
        INSERT INTO user_role (
          user_id,
          role_id,
          role_name,
          deleted_at,
          created_at,
          updated_at)
        VALUES (
          #{user_id},   //フィールド名を直接指定
          #{role_id},
          #{role_name},
          null,
          now(),
          now()
        )
    """)
    fun insertDefaultRoles(dto: UserRole)

(注意) 引数が2つ以上の場合➡︎変数名の指定が必要

UserRoleMapper.kt
    @Insert("""
        INSERT INTO user_role (
          user_id,
          role_id,
          role_name,
          deleted_at,
          created_at,
          updated_at)
        VALUES (
          #{dto.user_id},   //引数が複数ある場合は、dto.user_id とする
          #{dto.role_id},
          #{roleName},
          null,
          now(),
          now()
        )
    """)
    fun insertDefaultRoles(dto: UserRole, roleName: String)
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?