java h2 in-memory database error: Table not found
java h2 in-memory database error: Table not found
我尝试谷歌搜索,但几乎所有此类问题的解决方案都是添加 ;DB_CLOSE_DELAY=-1
,但它对我没有任何解决方案。
这是我的测试class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Main.class})
public class Testas {
@Autowired
@Qualifier("managerImplementation")
private ClassifierManager manager;
@Test
public void testManager(){
ClassifierGroupEntity cge = new ClassifierGroupEntity();
manager.saveClassifierGroup(cge);
}
}
经理class
@Service("managerImplementation")
public class ClassifierManagerImpl implements ClassifierManager{
@Autowired
private ClassifierGroupEntityRepository groupEntityRepository;
@Autowired
private ClassifierEntityRepository entityRepository;
@Autowired
private ClassifierValueEntityRepository valueEntityRepository;
@Override
public ClassifierGroupEntity getClassifierGroup(long id) {
return groupEntityRepository.findOne(id);
}
@Override
public ClassifierGroupEntity getClassifierGroup(String code) {
return groupEntityRepository.findByCode(code);
}
@Override
public ClassifierGroupEntity saveClassifierGroup(ClassifierGroupEntity entity) {
return groupEntityRepository.save(entity);
}
@Override
public void deleteClassifierGroup(long id) {
groupEntityRepository.delete(id);
}
@Override
public ClassifierEntity getClassifier(long id) {
return entityRepository.findOne(id);
}
@Override
public ClassifierEntity getClassifier(String code) {
return entityRepository.findByCode(code);
}
@Override
public ClassifierEntity saveClassifier(ClassifierEntity entity) {
return entityRepository.save(entity);
}
@Override
public void deleteClassifier(long id) {
entityRepository.delete(id);
}
@Override
public ClassifierValueEntity getClassifierValue(long id) {
return valueEntityRepository.findOne(id);
}
@Override
public ClassifierValue getClassifierValue(String classifiedCode, String valueCode) {
return null;
}
@Override
public ClassifierValueEntity saveClassifierValue(ClassifierValueEntity entity) {
return valueEntityRepository.save(entity);
}
@Override
public void deleteClassifierValue(long id) {
valueEntityRepository.delete(id);
}
}
最后是属性文件
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.user=sa
spring.datasource.password=
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
启动测试让我感到震惊
org.h2.jdbc.JdbcSQLException: Table "CLASSIFIER_GROUP_ENTITY" not found; SQL statement:
insert into classifier_group_entity (id, code, modified_details, modified_time, modified_user_id, order, revision, valid_details, valid_from, valid_till, parent_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [42102-191]
我不知道我是否应该提供其他任何东西,如果需要请告诉我。感谢您的提前帮助。
Table 没有找到,因为在尝试创建它时开始时出错。错误是由于 ClassifierGroupEntity
字段之一被命名为 'order',这是 SQL 中的保留字之一,因此生成了 SQL 语句Spring 的语法不正确。
我在 table 中添加了几列后得到了这个。持久性生成器在我的实体中创建了更多字段。
我在日志中注意到了这一点。
ERROR 13691 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table comment
ERROR 13691 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : Scale($"0") must not be bigger than precision("-1")
事实证明,当我的 IDE 的持久性生成器映射我的 TEXT 字段时,它给它的长度为 -1。
@Basic
@Column(name = "address", nullable = true, length = -1)
public String getAddress() {
return address;
}
删除长度属性解决了我的问题。
在 src/test/resources 文件夹中的 application.properties 文件中包含这些属性:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-自动=创建
这必须被覆盖,因为它没有保留 create 而是保留 none 或 在创建第一个 ddl 后验证。
我在为自己的应用程序编写单元测试时遇到了同样的问题。经过多次尝试,我无法获得我的应用程序测试用例 运行。我是 运行 版本 1.4.191,然后我升级到 1.4.196,它开始工作了。
就我而言,我很愚蠢,忘记实际输入 spring.datasource.url
出于某种原因,如果 JPA 找不到实际的数据库,它不会抛出错误 url。奇怪
在我的例子中,我在定义查询时对列使用小写语法,但 H2 数据库中的列是以大写形式定义的。我期望忽略区分大小写,但 H2 数据库并非如此。
在编写集成测试之前,我总是对 H2 数据库、Flyway、Spring Boot JPA 使用以下配置 - https://gist.github.com/vslala/d412156e5840fafa1b9f61aae5b20951
将以下配置放入您的 src/test/resources/application.properties
文件中。
# Datasource configuration for jdbc h2
# this is for file based persistent storage
# spring.datasource.url=jdbc:h2:file:/data/demo
# For in-memory storage
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=vslala
spring.datasource.password=simplepass
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# This has to be over-ridden because
# it's not kept create rather kept none or validate after the first ddl creation.
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
# This is for FlyWay configuration
spring.flyway.url=jdbc:h2:mem:testdb
spring.flyway.schemas=testdb
spring.flyway.user=vslala
spring.flyway.password=simplepass
这个问题请试试这个-
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;
我可能来晚了一点,但我遇到了完全相同的错误,我尝试了这里和其他网站上提到的几乎所有解决方案,例如 DATABASE_TO_UPPER=false;DB_CLOSE_DELAY =-1; DB_CLOSE_ON_EXIT=假;忽略大小写=真
但对我来说没有任何效果。
对我有用的是 将 data.sql 重命名为 import.sql
我在这里找到了 -
或
对于 Spring Boot 2.4+ 使用 spring.jpa.defer-datasource-initialization=true in application.properties(此处提到 - )
我意识到其他解决方案更合乎逻辑,但其中 none 对我有效,而这个确实有效。
添加spring.jpa.defer-datasource-initialization=true
不要忘记在应用程序中添加这些值。properties/application.yaml 文件
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true
我尝试谷歌搜索,但几乎所有此类问题的解决方案都是添加 ;DB_CLOSE_DELAY=-1
,但它对我没有任何解决方案。
这是我的测试class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Main.class})
public class Testas {
@Autowired
@Qualifier("managerImplementation")
private ClassifierManager manager;
@Test
public void testManager(){
ClassifierGroupEntity cge = new ClassifierGroupEntity();
manager.saveClassifierGroup(cge);
}
}
经理class
@Service("managerImplementation")
public class ClassifierManagerImpl implements ClassifierManager{
@Autowired
private ClassifierGroupEntityRepository groupEntityRepository;
@Autowired
private ClassifierEntityRepository entityRepository;
@Autowired
private ClassifierValueEntityRepository valueEntityRepository;
@Override
public ClassifierGroupEntity getClassifierGroup(long id) {
return groupEntityRepository.findOne(id);
}
@Override
public ClassifierGroupEntity getClassifierGroup(String code) {
return groupEntityRepository.findByCode(code);
}
@Override
public ClassifierGroupEntity saveClassifierGroup(ClassifierGroupEntity entity) {
return groupEntityRepository.save(entity);
}
@Override
public void deleteClassifierGroup(long id) {
groupEntityRepository.delete(id);
}
@Override
public ClassifierEntity getClassifier(long id) {
return entityRepository.findOne(id);
}
@Override
public ClassifierEntity getClassifier(String code) {
return entityRepository.findByCode(code);
}
@Override
public ClassifierEntity saveClassifier(ClassifierEntity entity) {
return entityRepository.save(entity);
}
@Override
public void deleteClassifier(long id) {
entityRepository.delete(id);
}
@Override
public ClassifierValueEntity getClassifierValue(long id) {
return valueEntityRepository.findOne(id);
}
@Override
public ClassifierValue getClassifierValue(String classifiedCode, String valueCode) {
return null;
}
@Override
public ClassifierValueEntity saveClassifierValue(ClassifierValueEntity entity) {
return valueEntityRepository.save(entity);
}
@Override
public void deleteClassifierValue(long id) {
valueEntityRepository.delete(id);
}
}
最后是属性文件
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.user=sa
spring.datasource.password=
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
启动测试让我感到震惊
org.h2.jdbc.JdbcSQLException: Table "CLASSIFIER_GROUP_ENTITY" not found; SQL statement:
insert into classifier_group_entity (id, code, modified_details, modified_time, modified_user_id, order, revision, valid_details, valid_from, valid_till, parent_id) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [42102-191]
我不知道我是否应该提供其他任何东西,如果需要请告诉我。感谢您的提前帮助。
Table 没有找到,因为在尝试创建它时开始时出错。错误是由于 ClassifierGroupEntity
字段之一被命名为 'order',这是 SQL 中的保留字之一,因此生成了 SQL 语句Spring 的语法不正确。
我在 table 中添加了几列后得到了这个。持久性生成器在我的实体中创建了更多字段。
我在日志中注意到了这一点。
ERROR 13691 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table comment
ERROR 13691 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : Scale($"0") must not be bigger than precision("-1")
事实证明,当我的 IDE 的持久性生成器映射我的 TEXT 字段时,它给它的长度为 -1。
@Basic
@Column(name = "address", nullable = true, length = -1)
public String getAddress() {
return address;
}
删除长度属性解决了我的问题。
在 src/test/resources 文件夹中的 application.properties 文件中包含这些属性:
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-自动=创建
这必须被覆盖,因为它没有保留 create 而是保留 none 或 在创建第一个 ddl 后验证。
我在为自己的应用程序编写单元测试时遇到了同样的问题。经过多次尝试,我无法获得我的应用程序测试用例 运行。我是 运行 版本 1.4.191,然后我升级到 1.4.196,它开始工作了。
就我而言,我很愚蠢,忘记实际输入 spring.datasource.url
出于某种原因,如果 JPA 找不到实际的数据库,它不会抛出错误 url。奇怪
在我的例子中,我在定义查询时对列使用小写语法,但 H2 数据库中的列是以大写形式定义的。我期望忽略区分大小写,但 H2 数据库并非如此。
在编写集成测试之前,我总是对 H2 数据库、Flyway、Spring Boot JPA 使用以下配置 - https://gist.github.com/vslala/d412156e5840fafa1b9f61aae5b20951
将以下配置放入您的 src/test/resources/application.properties
文件中。
# Datasource configuration for jdbc h2
# this is for file based persistent storage
# spring.datasource.url=jdbc:h2:file:/data/demo
# For in-memory storage
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=vslala
spring.datasource.password=simplepass
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# This has to be over-ridden because
# it's not kept create rather kept none or validate after the first ddl creation.
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
# This is for FlyWay configuration
spring.flyway.url=jdbc:h2:mem:testdb
spring.flyway.schemas=testdb
spring.flyway.user=vslala
spring.flyway.password=simplepass
这个问题请试试这个-
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MySQL;DB_CLOSE_DELAY=-1;IGNORECASE=TRUE;
我可能来晚了一点,但我遇到了完全相同的错误,我尝试了这里和其他网站上提到的几乎所有解决方案,例如 DATABASE_TO_UPPER=false;DB_CLOSE_DELAY =-1; DB_CLOSE_ON_EXIT=假;忽略大小写=真
但对我来说没有任何效果。 对我有用的是 将 data.sql 重命名为 import.sql
我在这里找到了 -
或
对于 Spring Boot 2.4+ 使用 spring.jpa.defer-datasource-initialization=true in application.properties(此处提到 - )
我意识到其他解决方案更合乎逻辑,但其中 none 对我有效,而这个确实有效。
添加spring.jpa.defer-datasource-initialization=true
不要忘记在应用程序中添加这些值。properties/application.yaml 文件
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true