java h2内存数据库错误:找不到表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36649179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:31:14  来源:igfitidea点击:

java h2 in-memory database error: Table not found

javaspringjdbch2

提问by Coderino Javarino

I tried googling around, but the solution to almost all this kind of questions was to add ;DB_CLOSE_DELAY=-1, however it does not solve anything for me.

我试着用谷歌搜索,但几乎所有这类问题的解决方案都是添加;DB_CLOSE_DELAY=-1,但它对我来说没有解决任何问题。

Here is my test 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);
    }
}

Manager 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);
    }


}

And finally properties file

最后是属性文件

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

Launching the test throws me

启动测试让我失望

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]

I don't know if I should provide anything else, please tell me if I do. I appreciate your help in advance.

我不知道我是否应该提供其他任何东西,如果我提供,请告诉我。我提前感谢您的帮助。

采纳答案by Coderino Javarino

After half a dozen hours down the drain I finally found solution, if anyone stumbles into similar problem.

经过六个小时的努力,我终于找到了解决方案,如果有人偶然发现了类似的问题。

Table was not found, because there was an error at the start when trying to create it. And the error was due to the fact, that one of the ClassifierGroupEntity fields was named 'order', which is one of reserved words in SQL, and thus the generated SQL statement by Spring was syntactical incorrect.

找不到表,因为在尝试创建它时在开始时出错。错误是由于 ClassifierGroupEntity 字段之一被命名为'order',这是 SQL 中的保留字之一,因此 Spring 生成的 SQL 语句在语法上不正确。

回答by Omoloro

I got this after adding a few columns to my table. The persistence generator created a few more fields in my entity.

我在表中添加了几列后得到了这个。持久性生成器在我的实体中创建了更多字段。

I noticed this in my logs.

我在日志中注意到了这一点。

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")

It turns out that the when my IDE's persistence generator mapped my TEXT field it gave it a length of -1.

事实证明,当我的 IDE 的持久性生成器映射我的 TEXT 字段时,它的长度为 -1。

@Basic
@Column(name = "address", nullable = true, length = -1)
public String getAddress() {
  return address;
}

Removing the length attribute solved the problem for me.

删除长度属性为我解决了这个问题。

回答by Ankit Bhatnagar

Have these properties in your application.propertiesfile in the src/test/resources folder:

在 src/ test/resources 文件夹中的application.properties文件中有这些属性:

spring.jpa.generate-ddl=true

spring.jpa.generate-ddl=true

spring.jpa.hibernate.ddl-auto=create

spring.jpa.hibernate.ddl-auto=创建

This has to be over-ridden because it's not kept createrather kept noneor validateafter the first ddl creation.

这必须被覆盖,因为在第一次 ddl 创建后,它不会保留create而是保留none验证

回答by Shreyash

I encountered the same problem while I was writing unit tests for my own application. After many tries, I was not able to get my application test cases running. I was running on version 1.4.191 and then I upgraded to 1.4.196 and it started working.

我在为自己的应用程序编写单元测试时遇到了同样的问题。经过多次尝试,我无法运行我的应用程序测试用例。我在 1.4.191 版本上运行,然后升级到 1.4.196 并开始工作。

回答by Nick08

In my case I was dumb and forgot to actually put in a spring.datasource.url

就我而言,我很笨,忘了实际放入spring.datasource.url

For some reason JPA doesn't throw an error if it can't find an actual database url. Weird

出于某种原因,如果找不到实际的数据库 url,JPA 不会抛出错误。奇怪的

回答by newday

In my case, I use lower case syntax for columns when the query is defined but the columns in the H2 database are defined in the upper case. I was expecting to ignore the case sensitivity but it is not the case with H2 databases.

就我而言,当定义查询时,我对列使用小写语法,但 H2 数据库中的列以大写形式定义。我希望忽略区分大小写,但 H2 数据库并非如此。

回答by vs_lala

I always use the below configuration for H2 Database, Flyway, Spring Boot JPA before writing integration tests - https://gist.github.com/vslala/d412156e5840fafa1b9f61aae5b20951

在编写集成测试之前,我总是对 H2 数据库、Flyway、Spring Boot JPA 使用以下配置 - https://gist.github.com/vslala/d412156e5840fafa1b9f61aae5b20951

Put below configuration in your src/test/resources/application.propertiesfile.

将以下配置放在您的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