NumberFormatException while H2 db insert in a table with primary key type is string

NumberFormatException while H2 db insert in a table with primary key type is string

我在 microsoft-sql-server 模式下使用 Spring boot 和 h2 db 作为集成测试用例 jdbc:h2:~/sample;MODE =MSSQL服务器 虽然 运行 测试用例 Table 已使用正确的数据类型

创建
@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))

while 运行 测试用例它尝试插入 db

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

低于异常

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.test.web.client.TestRestTemplate': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateBuilder' defined in class path resource [org/springframework/boot/autoconfigure/web/WebClientAutoConfiguration$RestTemplateConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.client.RestTemplateBuilder]: Factory method 'restTemplateBuilder' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration$$EnhancerBySpringCGLIB$62ac5b]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jacksonHttpMessageConverter' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter]: Factory method 'jacksonHttpMessageConverter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'config' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.rest.core.config.RepositoryRestConfiguration]: Factory method 'config' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositories' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.repository.support.Repositories]: Factory method 'repositories' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'teamMemberRepository': Cannot create inner bean '(inner bean)#202fd4c4' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#202fd4c4': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of URL [file:/C:/Users/myprojectpath/target/test-classes/data.sql]: insert INTO SCH.TeamMemberType (TeamMemberCode , Code) values ( 'Dev', 'Developer'); nested exception is org.h2.jdbc.JdbcSQLException: Data conversion error converting "Dev"; SQL statement:

Caused by: org.h2.jdbc.JdbcSQLException: Data conversion error converting "Dev"; SQL statement: insert INTO SCH.TeamMemberType (TeamMemberCode , Code) values ( 'Dev', 'Developer') Caused by: java.lang.NumberFormatException: For input string: "Dev"

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

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