H2 select: 意外的类型 39 映射

H2 select: unexpected type 39 mapping

我在 MySQL 模式下遇到 H2 数据库映射问题。

CREATE TABLE `USER_BOOKING` (
  `id`         bigint(20) unsigned  NOT NULL AUTO_INCREMENT,
  `booker_id`  varchar(36)          NOT NULL,
  `booking_id` varchar(20)          NOT NULL,
  UNIQUE KEY `id_booker_with_booking` (`booker_id`,`booking_id`),
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

使用 MyBatis 来 select 这样的数据:

@Select(" select (`booker_id`, `booking_id`) "
        + " from `USER_BOOKING` "
        + "where `booker_id` = #{bookerId};")
@Results(value = {
    @Result(column = "booker_id", property = "bookerId", javaType = String.class, jdbcType = JdbcType.VARCHAR),
    @Result(column = "booking_id", property = "bookingId", javaType = String.class, jdbcType = JdbcType.VARCHAR),
})
List<UserActivityModel> getBookerActivity(@Param("bookerId") String bookerId);

调用 getBookerActivity 以这个异常结束:

### Error querying database.  Cause: org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
### The error may exist in foo/bar/user/activity/infrastructure/UserActivityWriteStorage.java (best guess)
### The error may involve foo.bar.user.activity.infrastructure.UserActivityWriteStorage.getBookerActivity
### The error occurred while handling results
### SQL: select (`booker_id`, `booking_id`)  from `USER_BOOKING` where `booker_id` = ?;
### Cause: org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
; uncategorized SQLException; SQL state [HY000]; error code [50000]; General error: "java.lang.RuntimeException: type=39" [50000-200]; nested exception is org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
org.springframework.jdbc.UncategorizedSQLException: 
### Error querying database.  Cause: org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
### The error may exist in foo/bar/user/activity/infrastructure/UserActivityWriteStorage.java (best guess)
### The error may involve foo.bar.user.activity.infrastructure.UserActivityWriteStorage.getBookerActivity
### The error occurred while handling results
### SQL: select (`booker_id`, `booking_id`)  from `USER_BOOKING` where `booker_id` = ?;
### Cause: org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
; uncategorized SQLException; SQL state [HY000]; error code [50000]; General error: "java.lang.RuntimeException: type=39" [50000-200]; nested exception is org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:89)
    ...
Caused by: org.h2.jdbc.JdbcSQLNonTransientException: General error: "java.lang.RuntimeException: type=39" [50000-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:505)
...
Caused by: java.lang.RuntimeException: type=39
    at org.h2.message.DbException.throwInternalError(DbException.java:293)
...

(插入效果很好)

Type 39ROW 类型,而我希望是 STRING(类型 13)。

我不明白为什么我会获得这个 ROW 映射:是否有我遗漏的参数调整?

SELECT (`booker_id`, `booking_id`) … returns H2 中 ROW 数据类型的一列(而 MySQL 根本不支持这种构造)。看起来你不小心添加了这些括号,你的查询应该是 SELECT `booker_id`, `booking_id` … 没有它们。