java - java.sql.SQLException:不正确的整数值:在执行 jpa repository.save() 时在 varchar 字段上
问题描述
MerchantConfig 实体 -
public class MerchantConfig {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long entityId;
private String entityType;
..
}
merchantConfig = new MerchantConfig();
merchantConfig.setEntityType(Enum.MerchantConfigType.MERCHANT_ID.v);
merchantConfigRespository.save(merchantConfig); //exception is thrown here, if .setEntityType() is done.
枚举定义 -
public enum MerchantConfigType {
MERCHANT_ID("merchant_id"),
USER_ID("user_id");
public final String v;
MerchantConfigType(String value) {
this.v = value;
}
}
我不知道为什么它说不正确的整数值?它是由 Hibernate 实体定义自动生成的 varchar,它是 String,用于该字段。
更新
表架构 - 部分 -
CREATE TABLE `MerchantConfig` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`entityId` bigint(20) DEFAULT NULL,
`entityType` varchar(255) DEFAULT NULL,
`merchantId` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8
例外
javax.persistence.RollbackException: Error while committing the transaction
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
..
Caused by: java.sql.SQLException: Incorrect integer value: 'merchant_id' for column 'entityType' at row 1
更新 2
Hibernate 自动创建日志 -
休眠:
create table MerchantConfig (
id bigint not null auto_increment,
entityId bigint,
entityType varchar(255)
) engine=InnoDB
解决方案
我以前遇到过同样的问题。在springBoot数据库配置文件中,在datasource url后面添加如下字符串:</p>
?useUnicode=true&characterEncoding=utf8
至少你的字符串代码转换不会出错。
推荐阅读
- python - 为什么我不能从我的烧瓶应用程序控制台日志数据?我正在使用神社
- javascript - NextJS - 不从生产中的节点模块导入样式
- google-cloud-platform - 允许 gcp 实例上的 imap 端口?
- css - 将链接颜色添加到 CSS 属性组
- java - 处理来自发布者的并行耗尽的数据
- java - 清除除顶部片段外的片段回栈
- python - 如何在 Dataframe 中实现“Year + 1”
- angular - Angular 8 @output EventEmitter 不允许发出对象
- flutter - 调用函数时 (_) 是什么意思?
- sql - 基于表变量的条件连接 SQL 查询