英文:
Error executing DDL alter table if exists task.city
问题
错误信息 Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement"
我不明白如何解决它,我已经寻找了解决方案,但我检查了我的数据库和实体,一切都是正确的。我是从头开始创建的数据库。我在 Postgresql 中工作。添加了错误日志。
属性配置
spring.datasource.url=jdbc:postgresql://localhost:5432/innotechnum
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.open-in-view=true
spring.jpa.show-sql=true
City 类
@Data
@Entity
@Table(name = "city", schema = "task")
public class City {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "id_region", nullable = false)
private Integer id_region;
@Column(name = "name", nullable = false)
private String name;
}
House 类
@Data
@Entity
@Table(name = "house", schema = "task")
public class House {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
@JoinColumn(name = "id")
private Set<City> city;
@OneToMany(mappedBy = "house")
private Set<Contract> contract;
@Column(name = "id_landlord", nullable = false)
private Long id_landlord;
@Column(name = "outside", nullable = false)
private String outside;
@Column(name = "rooms", nullable = false)
private Integer rooms;
@Column(name = "price", nullable = false)
private Double price;
@Column(name = "description", nullable = false)
private String description;
}
日志:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
...
这是 PostgreSQL 给出的代码:
CREATE TABLE task.city
(
id integer NOT NULL DEFAULT nextval('task.city_id_seq'::regclass),
id_region integer NOT NULL,
name character varying(250) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT city_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE task.city
OWNER to root;
英文:
The error Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement"
I Don't understand how to solve it, I was already looking for a solution, but I check my database and Entity, everything is correct. I created the database from scratch myself. I work in Postgresql. Added the error log.
Properties
spring.datasource.url=jdbc:postgresql://localhost:5432/innotechnum
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.open-in-view= true
spring.jpa.show-sql=true
City
@Data
@Entity
@Table(name = "city", schema = "task")
public class City {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "id_region", nullable = false)
private Integer id_region;
@Column(name = "name", nullable = false)
private String name;
}
house
@Data
@Entity
@Table (name = "house", schema = "task")
public class House {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH
})
@JoinColumn(name = "id")
private Set<City> city;
@OneToMany(mappedBy = "house")
private Set<Contract> contract;
@Column(name = "id_landlord", nullable = false)
private Long id_landlord;
@Column(name = "outside", nullable = false)
private String outside;
@Column(name = "rooms", nullable = false)
private Integer rooms;
@Column(name = "price", nullable = false)
private Double price;
@Column(name = "description", nullable = false)
private String description;
}
LOGS:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
at
...
This is the code that Postgresql gives me:
CREATE TABLE task.city
(
id integer NOT NULL DEFAULT nextval('task.city_id_seq'::regclass),
id_region integer NOT NULL,
name character varying(250) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT city_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE task.city
OWNER to root;
答案1
得分: 2
你不应该使用单向的 @OneToMany 关联。尝试将以下内容添加到 City 类中:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "house_id")
private House house;
另外,你确定需要从房屋到城市的一对多关联而不是反过来吗?一个城市可以拥有多座房屋,但一座房屋属于特定的城市。
英文:
You should not use unidirectional @OneToMany association. Try to add the
@ManyToOne(fetchType = LAZY)
@JoinColumn(name = "house_id")
private House house;
to the City class.
P.S. Do you sure you need one-to-many association from house to city but not vice versa? City may have many houses, but house belongs to the particular city.
答案2
得分: 1
嗯,请尝试:
public class House {
...
@ManyToOne(optional = false)
@JoinColumn(name = "city_id", referencedColumnName = "id")
private Set<City> city;
...
}
顺便提一下,
- 实体类中不应使用@Data lombok
- 将 spring.jpa.hibernate.ddl-auto=update 中的
update
改为none
。您应该自行管理模式。
英文:
Hmm, try
public class House {
...
@ManyToOne(optional = false)
@JoinColumn(name = "city_id", referencedColumnName = "id")
private Set<City> city;
...
}
btw,
- should not use @Data lombok in an entity class
- spring.jpa.hibernate.ddl-auto=update can change
update
tonone
. You should manage schema on your own.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论