sql-server - 当我添加 sql 脚本时,Spring Boot 不起作用

标签 sql-server spring postgresql spring-boot

我有一个简单的 Restfull 应用程序。有用。但是,当我添加 .sql 文件时 - 该应用程序无法运行。 我添加 schema.sql

CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;

CREATE TABLE country (
  id SERIAL PRIMARY KEY NOT NULL ,
  county_name CHAR (30) NOT NULL UNIQUE );

CREATE TABLE city (
  id SERIAL PRIMARY KEY NOT NULL ,
  city_name CHAR (30) NOT NULL,
  country_id INTEGER REFERENCES country,
  UNIQUE (city_name, country_id));

CREATE TABLE street (
  id SERIAL PRIMARY KEY NOT NULL,
  street_name CHAR (30) NOT NULL,
  city_id INTEGER REFERENCES city,
  building_number CHAR(10),
  UNIQUE (street_name, city_id));

和data.sql

INSERT INTO country (county_name) VALUES ('Ukraine');
INSERT INTO country (county_name) VALUES ('France');
INSERT INTO country (county_name) VALUES ('Italy');
INSERT INTO country (county_name) VALUES ('German');
INSERT INTO country (county_name) VALUES ('England');

应用程序属性

spring.jpa.database=postgresql
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/ubr
spring.datasource.username=postgres
spring.datasource.password=1
server.port=8080

第一次运行后 - 它没有错误,但第二次运行后我有 a stacktrace with many exeptions .我做错了什么?

最佳答案

您正在混合两种不同的功能。

spring.jpa.hibernate.ddl-auto=create-drop 在您希望 Hibernate 自动更新架构时运行。

schema.sqldata.sql 管理是 Spring Boot 的一个特性。 doc is pretty explicit about JPA usage

If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.

关于sql-server - 当我添加 sql 脚本时,Spring Boot 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422031/

相关文章:

sql - 查询查询语法错误

java - Spring 数据 JPA @EnableJpaRepositories TypeNotPresentExceptionProxy

java - ClassLoader 不提供 'addTransformer(ClassFileTransformer)' 方法

postgresql - CMDBuild密码重置

postgresql - 关于 PostgreSQL 内部工作原理的书

sql - SQL Server PK 最佳实践

sql-server - 如何在sql中使用exec字符串分配变量?

sql-server - 从 2017 年到 2018 年的 TFS 升级

java - 执行 Cucumber 测试时 Spring Boot 休息服务关闭

postgresql - 如何使用 JNDI 创建 BIRT 报告数据源?