我使用Spring JPA和Hibernate连接到我的Heroku PostgreSQL数据库。学生:
@Entity(name = "Student")
@Table(
name = "Student",
uniqueConstraints = {
@UniqueConstraint(name = "student_email_unique", columnNames = "email")
}
)
public class Student {
...
这是我的应用。属性文件:
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 =总是指向应用程序属性文件,因为这不是内存中的数据库。日志如下:
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:Userskalioeclipse-workspacedemotargetclasses started by kalio in C:Userskalioeclipse-workspacedemo)
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)
我在Postgresql 13中遇到了同样的问题。我有四个表,其中三个表之间有manymany关系,第四个表上有onemany关系。创建所有四个表的序列,但不创建表。
在启动时,我得到以下输出:
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中的一个bug。