0
votes

I am using Spring boot and h2 db for integration test case in microsoft-sql-server mode jdbc:h2:~/sample;MODE=MSSQLServer While running test case Table has been created with proper data type

@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;
}

Console:

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

while running test case it try to insert in db

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

getting below exception

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$$4162ac5b]: 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"

why h2 db knowingly [TeamMemberCode varchar(255) identity] converting "Dev" as Number

1

1 Answers

1
votes

Remove the @GeneratedValue(strategy = GenerationType.IDENTITY) as you don't want Hibernate to generate ID for you, since your ID is a String. You have have to set it manually during each insert operation