当我运行Spring Boot应用程序时,它不执行schema.sql脚本。我在这里做错什么了吗?
application.properties:
spring.security.user.name=user
spring.security.user.password=asdf
spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
我还尝试将其添加到我的application.properties
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-h2.sql
build.gradle
dependencies {
implementation('org.springframework.session:spring-session-jdbc')
implementation('org.springframework.boot:spring-boot-starter-data-rest')
implementation('org.springframework.boot:spring-boot-starter-jdbc')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation('org.springframework.session:spring-session-jdbc')
runtimeOnly('com.h2database:h2')
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation('org.springframework.security:spring-security-test')
}
schema.sql
DROP TABLE IF EXISTS SPRING_SESSION_ATTRIBUTES;
DROP TABLE IF EXISTS SPRING_SESSION;
CREATE TABLE SPRING_SESSION (
PRIMARY_ID CHAR(36) NOT NULL,
SESSION_ID CHAR(36) NOT NULL,
CREATION_TIME BIGINT NOT NULL,
LAST_ACCESS_TIME BIGINT NOT NULL,
MAX_INACTIVE_INTERVAL INT NOT NULL,
EXPIRY_TIME BIGINT NOT NULL,
PRINCIPAL_NAME VARCHAR(100),
CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
);
CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);
CREATE TABLE SPRING_SESSION_ATTRIBUTES (
SESSION_PRIMARY_ID CHAR(36) NOT NULL,
ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
ATTRIBUTE_BYTES LONGVARBINARY NOT NULL,
CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
);
春季版:2.1.0
答案 0 :(得分:1)
您是否关闭了休眠ddl-auto选项?如果没有将其放在您的application.properties
中spring.jpa.hibernate.ddl-auto=none
spring docs中的第85.3节说
在基于JPA的应用程序中,您可以选择让Hibernate创建架构 或使用schema.sql,但您不能两者都做。确保禁用 如果使用schema.sql,则为spring.jpa.hibernate.ddl-auto。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以同时使用休眠自动创建和schema.xml,并且可能需要同时使用它们,但是它们不应重叠。如果您在application.xml
中为休眠声明默认模式,如下所示:
spring:
jpa:
hibernate:
ddl-auto: create-drop
default_schema: NASA_SPACE_0
... hibernate不会创建模式NASA_SPACE_0,因此schema.xml
是此语句的理想位置,并且可以与hibernate的ddl-auto创建一起使用:
create schema `NASA_SPACE_0`;