首页 > 解决方案 > 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

标签: javahibernate

解决方案


我以前遇到过同样的问题。在springBoot数据库配置文件中,在datasource url后面添加如下字符串:</p>

?useUnicode=true&characterEncoding=utf8

至少你的字符串代码转换不会出错。


推荐阅读