【问题标题】:How can I populate correctly H2 database with custom script automatically?如何自动使用自定义脚本正确填充 H2 数据库?
【发布时间】:2022-12-21 09:41:27
【问题描述】:

假设我有:

spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=truelogging.level.org.hibernate.SQL=DEBUG
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto=create# Show or not log for each sql query
spring.jpa.show-sql = truespring.jpa.generate-ddl=true
spring.jpa.defer-datasource-initialization=true

我的脚本是data.sql

INSERT INTO ITEMS(ITEM_ID, value) VALUES(1, 'EXAMPLE');

第二个脚本schema.sql

create table items
(
item_id int not null auto_increment,
value varchar(50) not null,
primary key (item_id)
);

问题是当我在运行项目时使用这些配置自动填充时,我遇到了完整的stacktrace 中的问题:

https://gist.github.com/invzbl3/abe68fe95c69b3a81699a2ed08375853#file-stacktrace-L111

有人可以告诉我,我在这里遗漏了什么吗?

任何聪明的想法在这里都会有所帮助。

如果我手动运行它,我没有任何问题,但是在运行项目以自动填充时,我遇到了堆栈跟踪中的问题。


更新:

我已经尝试过这个,例如:

  1. https://stackoverflow.com/a/66333222/8370915

    通过更改实体而不是此变体:

    @Entity
    @Table(name = "ITEMS")
    public class Item {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        private Long id;
    
        private String value;
    }
    

    另一个是:

    @Entity
    @Table(name = "`ITEMS`")
    public class Item {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        private Long id;
    
        private String value;
    }
    

    结果我有: https://gist.github.com/invzbl3/83f00b9ca8d536052ac3174f7f9ddf47#file-stacktrace-L111

    1. 我试过这个: https://stackoverflow.com/a/44267377/8370915

    通过更改 sql 脚本:

    代替:

    INSERT INTO ITEMS(item_id, value)
    VALUES (1, 'EXAMPLE');
    

    到:

    INSERT INTO ITEMS(item_id, value) VALUES (1, '`EXAMPLE`');
    

    结果我收到:https://gist.github.com/invzbl3/ae873cf7aaeeccfedff2dc5c8f543773#file-stacktrace-2-L111

【问题讨论】:

  • VALUE 是 SQL 的保留字,是 H2 中的关键字,您需要将其引用为 "VALUE""value"(或 MySQL 样式的 `value`)。
  • @EvgenijRyazanov,非常感谢您指出!稍后我将在这里分享新的调查和更新。感谢您花时间检查它。这对我有很大帮助。

标签: java h2 in-memory


【解决方案1】:

经过大量的尝试、调查、阅读不同的文章,我终于找到了如何做到这一点的方法,那么如何根据问题中的要求使用H2数据库和专门使用in-memory模式的脚本来做到这一点。

因此,要在 in-memory 模式下使用 H2 数据库自动运行脚本,您需要将此代码用作:

项目.java

@Entity(name = "ITEM_ENTITY")
@Table(name = "items")
public class Item {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String designation;
}

主程序.java

@SpringBootApplication
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

应用程序.properties

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.datasource.driverClassName=org.h2.Driver
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.session.jdbc.initialize-schema=always
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.defer-datasource-initialization=true
spring.sql.init.continue-on-error=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.sql.init.mode=always

数据.sql

insert into ITEMS(id, designation)
values (1, 'EXAMPLE');

模式.sql

create table items
(
    id                    int not null auto_increment,
    designation           varchar(50) not null,
    primary key (id)
);

有用的文章:

  1. https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/howto-database-initialization.html
  2. https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization.using-basic-sql-scripts
  3. https://www.baeldung.com/running-setup-logic-on-startup-in-spring

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2019-07-01
    • 2022-08-18
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多