我有一个H2数据库的URL“jdbc: H2:test”。我使用create table PERSON (ID INT主键,名字VARCHAR(64),姓VARCHAR(64));;然后使用select * from PERSON从这个(空)表中选择所有内容。到目前为止,一切顺利。

然而,如果我将URL更改为“jdbc:h2:mem:test”,唯一的区别是数据库现在只在内存中,这给了我一个org.h2.jdbc。JdbcSQLException:表“PERSON”未找到;SQL语句:SELECT * FROM PERSON[42102-154]。我可能错过了一些简单的东西,但任何帮助将不胜感激。


当前回答

<bean id="benchmarkDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

其他回答

在添加Spring Data JPA依赖项后,我发现它可以正常工作

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>

在应用程序中添加H2 DB配置。yml:

spring:
  datasource:
    driverClassName: org.h2.Driver
    initialization-mode: always
    username: sa
    password: ''
    url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
  h2:
    console:
      enabled: true
      path: /h2
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: none

我也遇到了同样的问题,并在应用程序测试中更改了配置。属性:

#Test Properties
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop

和我的依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.198</version>
        <scope>test</scope>
    </dependency>

以及测试类上使用的注释:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
public class CommentServicesIntegrationTests {
...
}
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "user" not found; SQL statement:

在我的情况下,我的表名是用户,但从H2 2.1.212用户是保留的,所以不能使表

@Table(name="users"

datasource:
  url: jdbc:h2:mem:testdb;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1;

现在起作用了

在我的例子中,我为表H2 Database中的列名使用了特殊的关键字。如果您正在使用不同的数据库,则避免在不同的数据库中使用这些特殊的关键字。Spring和Hibernate不够聪明,不能准确地告诉您哪些列名是禁止的,或者在表创建过程中哪里出现了错误。 关键词如;

描述,间隔,度量

为了解决我遇到的问题,我将这些字段重命名为:

Descr, time_interval, time_metric

http://www.h2database.com/html/advanced.html

我可能有点晚了,但我遇到了完全相同的错误,我尝试了这里提到的每个解决方案和其他网站,如DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT = FALSE;IGNORECASE = TRUE

但对我来说什么都不管用。 对我有用的是重命名数据。导入。SQL

我在这里找到的——https://stackoverflow.com/a/53179547/8219358

Or

对于Spring Boot 2.4+,在应用程序中使用Spring .jpa.defer-datasource-initialization=true。属性(此处提到- https://stackoverflow.com/a/68086707/8219358)

我意识到其他解决方案更合乎逻辑,但没有一个对我有效,而这个却管用。