【问题标题】:Spring JPA and Hibernate not auto-creating PostgreSQL db tablesSpring JPA 和 Hibernate 不会自动创建 PostgreSQL 数据库表
【发布时间】:2021-10-21 15:52:08
【问题描述】:

我正在使用 Spring JPA 和 Hibernate 连接到我的 Heroku PostgreSQL 数据库。我写了一个班级学生:

@Entity(name = "Student")
@Table(
    name = "Student",
    uniqueConstraints = {
            @UniqueConstraint(name = "student_email_unique", columnNames = "email")
    }
)
public class Student {
...

这是我的 application.properties 文件:

spring.datasource.url=jdbc:postgresql://<host>:<port>/<dbname>
spring.datasource.username=<user>
spring.datasource.password=<pwd>
spring.datasource.driver-class-name=org.postgresql.Driver

spring.sql.init.mode=always
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.hbm2ddl.auto=create

javax.persistence.schema-generation.database.action=create

spring.jpa.generate.ddl-auto=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.defer-datasource-initialization=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.properties.hibernate.format_sql=true

我的应用程序正确连接到数据库,但未自动创建学生表。我将 spring.sql.init.mode=always 添加到应用程序属性文件中,因为这不是内存数据库。日志如下:

2021-08-19 21:18:05.418  INFO 2224 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 1.8.0_231 on LAPTOP-08SELGFV with PID 2224 (C:\Users\kalio\eclipse-workspace\demo\target\classes started by kalio in C:\Users\kalio\eclipse-workspace\demo)
2021-08-19 21:18:05.434  INFO 2224 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2021-08-19 21:18:09.599  INFO 2224 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-08-19 21:18:09.661  INFO 2224 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 28 ms. Found 0 JPA repository interfaces.
2021-08-19 21:18:14.705  INFO 2224 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-08-19 21:18:14.742  INFO 2224 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-08-19 21:18:14.743  INFO 2224 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-19 21:18:15.083  INFO 2224 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-08-19 21:18:15.084  INFO 2224 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 9213 ms
2021-08-19 21:18:16.894  INFO 2224 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2021-08-19 21:18:20.575  INFO 2224 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2021-08-19 21:18:20.737  INFO 2224 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-08-19 21:18:21.032  INFO 2224 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.32.Final
2021-08-19 21:18:21.749  INFO 2224 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-08-19 21:18:22.376  INFO 2224 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
2021-08-19 21:18:38.310  INFO 2224 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-08-19 21:18:38.376  INFO 2224 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-08-19 21:18:38.809  WARN 2224 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-08-19 21:18:42.049  INFO 2224 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-19 21:18:42.177  INFO 2224 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 39.466 seconds (JVM running for 41.782)

【问题讨论】:

  • 有错别字,请使用spring.jpa.hibernate.ddl-auto
  • 我纠正了错字,但问题仍然存在:/
  • spring.jpa.generate.ddl-auto=true 错误,不存在这样的密钥。可能你想写:spring.jpa.generate-ddl=true,因为你的下一行是spring.jpa.hibernate.ddl-auto=create-drop,这没有用。因此,请尝试从属性文件中完全删除行 spring.jpa.generate.ddl-auto=true
  • 我也删除了它,它没有自动创建。出于安全原因,我正在阅读有关此自动创建不适用于 postgresql 而不是内存数据库的内容,其中只有某些用户可以执行此任务。
  • 您是如何确定该表不存在的?我有同样的问题,但它是 pgadmin3 显示没有表并抛出错误。 Intellij 数据库浏览器显示我的表在那里(见我的“答案”和下面的评论)。 pgadmin3 完全不可靠。

标签: spring postgresql hibernate spring-data-jpa heroku-postgres


【解决方案1】:

我在使用 Postgresql 13 时遇到了同样的问题。我有四个表,三个表之间存在多对多关系,第四个表上存在一个 OneToMany。已创建所有四个表的序列,但未创建表。

在启动时,我在输出中得到以下信息:

Hibernate: create table account (id  bigserial not null, account_id varchar(255), opened_date date, primary key (id))
Hibernate: create table account_person_set (account_id int8 not null, person_set_id int8 not null, primary key (account_id, person_set_id))
Hibernate: create table person (id  bigserial not null, account_id int8, primary key (id))
Hibernate: create table person_reservation_set (person_id int8 not null, reservation_set_id int8 not null, primary key (person_id, reservation_set_id))
Hibernate: create table reservable_resource (id  bigserial not null, description varchar(255), primary key (id))
Hibernate: create table reservable_resource_reservation_set (reservable_resource_id int8 not null, reservation_set_id int8 not null, primary key (reservable_resource_id, reservation_set_id))
Hibernate: create table reservation (id  bigserial not null, comments varchar(255), primary key (id))
Hibernate: create table reservation_person_set (reservation_id int8 not null, person_set_id int8 not null, primary key (reservation_id, person_set_id))
Hibernate: create table reservation_resources (reservation_id int8 not null, resources_id int8 not null, primary key (reservation_id, resources_id))
Hibernate: alter table account_person_set add constraint UK_qortuq7nc9ytbjd1najldg362 unique (person_set_id)
Hibernate: alter table account_person_set add constraint FKqvhfkuu4thxedfntcw0kjemlt foreign key (person_set_id) references person
Hibernate: alter table account_person_set add constraint FK47nbhl58befqf3qkvnhd3ybtw foreign key (account_id) references account
Hibernate: alter table person add constraint FKiyv5708nn4k5w34ph5dofk6ag foreign key (account_id) references account
Hibernate: alter table person_reservation_set add constraint FK74dftwa1nmgx7h4cgqsvexcnu foreign key (reservation_set_id) references reservation
Hibernate: alter table person_reservation_set add constraint FKbd4umv4bsu5415jlksxgteyt8 foreign key (person_id) references person
Hibernate: alter table reservable_resource_reservation_set add constraint FK9u01dt439c84km39klflxk2pl foreign key (reservation_set_id) references reservation
Hibernate: alter table reservable_resource_reservation_set add constraint FKr3s6qquluyccbo3qg00gdr8bb foreign key (reservable_resource_id) references reservable_resource
Hibernate: alter table reservation_person_set add constraint FK4vrllnympyak4snbaouipos17 foreign key (person_set_id) references person
Hibernate: alter table reservation_person_set add constraint FKicbdc2an5qhuiwwem0lcc75wp foreign key (reservation_id) references reservation
Hibernate: alter table reservation_resources add constraint FKeee0ergn0dmv0yx91ir13cl0w foreign key (resources_id) references reservable_resource
Hibernate: alter table reservation_resources add constraint FK3ibmdb9y8dq9w9ih59ttfc86 foreign key (reservation_id) references reservation

看起来它正在生成正确的 DDL,但没有执行它。

这看起来像是 Spring 中的一个错误。

【讨论】:

  • 这是 pgadmin3 中的一个错误。当我尝试查看表格时,它没有显示表格并抛出错误。我在 Intellij 中配置了数据库浏览器,表就在那里。 pgadmin3 漏洞百出,应该删除。 pgadmin4 更糟。它根本不运行。
  • 我必须使用资源文件夹中的 schema.sql 手动创建表。无法获取 postgres 的自动生成表
猜你喜欢
  • 2016-11-12
  • 2021-09-15
  • 2016-05-26
  • 2019-07-18
  • 2022-08-18
  • 1970-01-01
  • 2022-08-12
  • 2013-12-04
  • 2011-05-29
相关资源
最近更新 更多