首页 文章

NumberFormatException,而H2 db插入主键类型的表是字符串

提问于
浏览
0

我在microsoft-sql-server模式下使用Spring启动和h2 db进行集成测试用例 jdbc:h2:~/sample;MODE=MSSQLServer 运行测试用例时表已经使用适当的数据类型创建

@Entity
@Table(name = "TeamMemberType", schema = "SCH")
public class TeamMemberType {
  @Id
  @Column(name = "TeamMemberCode", unique = true, nullable = false)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private String teamMemberCode;

  @Column(name = "Code")
  private String code;
}

安慰:

Hibernate: create table SCH.TeamMemberType (TeamMemberCode varchar(255) identity not null, Code varchar(255))

在运行测试用例时,它尝试在db中插入

insert INTO SCH.TeamMemberType (TeamMemberCode , Code) values ( 'Dev', 'Developer')

低于例外

java.lang.IllegalStateException:无法加载ApplicationContext引起:org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.boot.test.web.client.TestRestTemplate'的bean时出错:bean的初始化失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名称为'restTemplateBuilder'的bean时出错[org / springframework / boot / autoconfigure / web / WebClientAutoConfiguration $ RestTemplateConfiguration.class]:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.boot.web.client.RestTemplateBuilder]:工厂方法'restTemplateBuilder'抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration'的bean时出错:通过构造函数的Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration $$ EnhancerBySpringCGLIB $$ 4162ac5b]:构造函数抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / data / rest / webmvc / config / RepositoryRestMvcConfiguration.class]中定义名称为'jacksonHttpMessageConverter'的bean创建错误:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter]:工厂方法'jacksonHttpMessageConverter'抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'config'的bean时出错[org / springframework / data / rest / webmvc / config / RepositoryRestMvcConfiguration.class]:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法'config'引发异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'repositories'的bean时出错[org / springframework / data / rest / webmvc / config / RepositoryRestMvcConfiguration.class]:通过工厂方法进行Bean实例化失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.repository.support.Repositories]:工厂方法'存储库'引发异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'teamMemberRepository'的bean时出错:设置bean时无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)#202fd4c4' property'entalManager';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)#202fd4c4'的bean时出错:在设置构造函数参数时无法解析对bean'entalManagerFactory'的引用;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'entityManagerFactory'的bean时出错:FactoryBean的单例对象的后处理失败;嵌套异常是org.springframework.jdbc.datasource.init.ScriptStatementFailedException:无法执行URL [file:/ C:/Users/myprojectpath/target/test-classes/data.sql]的SQL脚本语句#1:插入INTO SCH .TeamMemberType(TeamMemberCode,Code)值('Dev','Developer');嵌套异常是org.h2.jdbc.JdbcSQLException:转换“Dev”的数据转换错误; SQL语句:引起:org.h2.jdbc.JdbcSQLException:转换“Dev”的数据转换错误; SQL语句:插入INTO SCH.TeamMemberType(TeamMemberCode,Code)值('Dev','Developer')引起:java.lang.NumberFormatException:对于输入字符串:“Dev”

为什么h2 db故意 [TeamMemberCode varchar(255) identity] 将“ Dev ”转换为 Number

1 回答

  • 1

    删除 @GeneratedValue(strategy = GenerationType.IDENTITY) ,因为您不希望Hibernate为您生成ID,因为您的ID是字符串 . 您必须在每次插入操作期间手动设置它

相关问题