我在Spring Boot(带有Postgresql DB)应用程序中在两个模型(car_model
和car_picture_model
)之间建立了OneToOne(单向)关系。从Java代码启动并插入数据后,我可以获得正确的关系信息:
id | make | car_image_id
1 | Ferrari | 2
我可以选择所有汽车和所有图像。 DBeaver同时显示表和关系箭头。
但是在日志中,我可以看到异常:
org.hibernate.tool.schema.spi.CommandAcceptanceException:通过JDBC语句执行DDL错误 在org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)〜[hibernate-core-5.2.17.Final.jar:5.2.17.Final] ... 在java.lang.Thread.run(Thread.java:748)〜[na:1.8.0_181] 引起原因:org.postgresql.util.PSQLException:错误:关系“ car_model”不存在 在org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)〜[postgresql-9.4-1206-jdbc42.jar:9.4]
完整日志:https://pastebin.com/ui1Cj99j
删除名称=“ cars”不会更改任何内容。
与使用car_name添加@Table注释相同
CarModel:
import javax.persistence.*;
@Entity(name = "cars")
public class CarModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String make;
@OneToOne
@JoinColumn(name = "car_image_id", unique = true)
private CarPictureModel carPictureModel;
// Getters and Setters
}
CarPictureModel:
import javax.persistence.*;
@Entity
public class CarPictureModel {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column
private String fileName;
@Column
private String fileType;
@Lob
@Column
private byte[] data;
// for (bidirectional mapping)
// @OneToOne(mappedBy = "carPictureModel")
// private CarModel carModel;
// Getters and Setters
}
在application.properties
中有两个:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
您能帮我一下,告诉我我在这里想念什么吗?
答案 0 :(得分:0)
似乎您缺少@Table批注。如果您的表名是car_model,请在类定义之前使用@Table(name =“ car_model”)。
与CarPictureModel相同。
@Entity(name = "cars")
@Table(name = "car_model")
public class CarModel {
}
@Entity
@Table(name = "car_picture_model")
public class CarPictureModel {
}
答案 1 :(得分:0)
@Rohit gave me a good hint.
I chaned spring.jpa.hibernate.ddl-auto=create-drop
to spring.jpa.hibernate.ddl-auto=create
and the problem is gone.
Now i got in logs:
Hibernate: alter table car_model drop constraint FKjlrfjf7bhyha1pkb0004q6l6w
Hibernate: drop table if exists car_model cascade
Hibernate: drop table if exists car_picture_model cascade
Hibernate: drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start 1 increment 1
Hibernate: create table car_model (id int4 not null, color varchar(255), make varchar(255), mfy int4, model varchar(255), car_image_id int4, primary key (id))
Hibernate: create table car_picture_model (id int4 not null, data oid, file_name varchar(255), file_type varchar(255), primary key (id))
So it looks like both create
and create-drop
first drops all tables before creating new one. When the problem occurred there were no tables to drop because create-drop
droped earlier all tables when stopping server with ctrl-c. As this logs shows:
SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed drop of schema as part of SessionFactory shut-down'
Hibernate: alter table car_model drop constraint FKjlrfjf7bhyha1pkb0004q6l6w
Hibernate: drop table if exists car_model cascade
Hibernate: drop table if exists car_picture_model cascade
Hibernate: drop sequence if exists hibernate_sequence