我在 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 (`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)
...
(插入效果很好)
类型 39
是一个 ROW
类型,而我期望的是 STRING
(类型 13
)。
我不明白为什么要获得此 ROW
映射:是否有我错过的参数调整?
最佳答案
SELECT (`booker_id`, `booking_id`) ...
返回 H2 中 ROW 数据类型的一列(MySQL 根本不支持这种构造)。看起来您不小心添加了这些括号,并且您的查询应该是 SELECT `booker_id`, `booking_id` ...
而不包含它们。
关于java - H2选择: unexpected type 39 mapping,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60741398/