我在Passport和PassportType之间具有单向一对一关系。我在映射上声明了cascade = CascadeType.ALL。
为什么DDL自动生成的外键约束fkh9lgnglfbx8bnvnom6ovyx89没有ON DELETE CASCADE?我错过了什么?
我正在使用Spring Boot 2.2.2。
在数据库中创建的DDL(已编辑)
CREATE TABLE public.passport
(
id bigint NOT NULL DEFAULT nextval('passport_id_seq'::regclass),
country_id bigint,
passport_type_id bigint,
CONSTRAINT passport_pkey PRIMARY KEY (id),
CONSTRAINT fkh9lgnglfbx8bnvnom6ovyx89 FOREIGN KEY (passport_type_id)
REFERENCES public.passport_type (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT fkqwoht23ddjewoktst0da7j9q7 FOREIGN KEY (country_id)
REFERENCES public.country (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
CREATE TABLE public.passport_type
(
id bigint NOT NULL DEFAULT nextval('passport_type_id_seq'::regclass),
description character varying(255) COLLATE pg_catalog."default",
name character varying(255) COLLATE pg_catalog."default",
CONSTRAINT passport_type_pkey PRIMARY KEY (id)
)
Passport.java
@Entity
public class Passport{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne(orphanRemoval=true, cascade=CascadeType.ALL)
@JoinColumn(name="country_id")
private Country country;
@OneToOne(orphanRemoval=true, cascade=CascadeType.ALL)
@JoinColumn(name="passport_type_id")
private PassportType passportType;
...
}
PassportType.java
@Entity
public class PassportType{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name; //values: Ordinary, Official, Diplomatic, Emergency, Collective, Family
private String description;
...
}
application.properties
# Database
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://localhost:5432/visa
spring.datasource.username=
spring.datasource.password=
spring.datasource.initialization-mode=always
# JPA
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
I think in Postgres it works the other way round。检查PassportType表中是否有ON DELETE CASCADE。