JPA @ForeignKey(value = ConstraintMode.NO_CONSTRAINT) 不适用于@ManyToMany
JPA @ForeignKey(value = ConstraintMode.NO_CONSTRAINT) not working with @ManyToMany
我有两个具有多对多关系的实体。这里的目标是在应用程序以无外键
开始时创建模式
1). Job.java
package com.govjobportalbackend.entity;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "job")
public class Job extends BasicEntity {
@Column(name = "icon")
private String icon;
@ManyToMany
@JoinTable(
name="job_city",
joinColumns = @JoinColumn(name = "job_id"),
inverseJoinColumns = @JoinColumn(name = "city_id"),
foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT),
inverseForeignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
)
private List<City> cities;
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
2). City.java
package com.govjobportalbackend.entity;
import java.util.List;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
@Entity
@DiscriminatorValue(value = "city")
public class City extends JobMetadata {
@ManyToMany(mappedBy = "cities")
private List<Job> jobs;
@Override
public List<Job> getJobs() {
return jobs;
}
@Override
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}
}
下面属性设置在application.properties文件
spring.jpa.hibernate.ddl-auto=update
当 运行 应用程序时,它在日志中记录 SQL 并创建两个外键
Hibernate: create table job (id int4 not null, name varchar(255), icon varchar(255), primary key (id))
Hibernate: create table job_city (job_id int4 not null, city_id int4 not null)
Hibernate: create table job_metadata (type varchar(31) not null, id int4 not null, name varchar(255), primary key (id))
Hibernate: alter table if exists job_city add constraint FKiksm0d31mc3osxut4ciaf4uof foreign key (job_id) references job
Hibernate: alter table if exists job_city add constraint FKknw4pf63xt1tvnqrmrjrm5hqq foreign key (city_id) references job_metadata
如果我在 City.java 中按照以下注释,那么它会按预期工作,但根据我的“小”研究,此错误已在休眠中修复(因此映射实体不需要使用折旧注释进行注释),或者可能是我错了。
@ManyToMany(mappedBy = "cities")
@org.hibernate.annotations.ForeignKey(name = "none")
private List<Job> jobs;
我使用的环境如下;
- Java11
- 休眠 5.4.28.Final (spring-boot-starter-web)
正如 SimonMartinelli 所指出的,这绝对是一个 Hibernate 错误。对我有用的版本是:
@JoinTable(
name="job_city",
joinColumns = @JoinColumn(name = "job_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)),
inverseJoinColumns = @JoinColumn(name = "city_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
)
我发现当您 (1) 使用 @JoinTable.foreignKey
或 (2) 省略 name
参数时,功能会中断。
我有两个具有多对多关系的实体。这里的目标是在应用程序以无外键
开始时创建模式1). Job.java
package com.govjobportalbackend.entity;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "job")
public class Job extends BasicEntity {
@Column(name = "icon")
private String icon;
@ManyToMany
@JoinTable(
name="job_city",
joinColumns = @JoinColumn(name = "job_id"),
inverseJoinColumns = @JoinColumn(name = "city_id"),
foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT),
inverseForeignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
)
private List<City> cities;
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public List<City> getCities() {
return cities;
}
public void setCities(List<City> cities) {
this.cities = cities;
}
}
2). City.java
package com.govjobportalbackend.entity;
import java.util.List;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
@Entity
@DiscriminatorValue(value = "city")
public class City extends JobMetadata {
@ManyToMany(mappedBy = "cities")
private List<Job> jobs;
@Override
public List<Job> getJobs() {
return jobs;
}
@Override
public void setJobs(List<Job> jobs) {
this.jobs = jobs;
}
}
下面属性设置在application.properties文件
spring.jpa.hibernate.ddl-auto=update
当 运行 应用程序时,它在日志中记录 SQL 并创建两个外键
Hibernate: create table job (id int4 not null, name varchar(255), icon varchar(255), primary key (id))
Hibernate: create table job_city (job_id int4 not null, city_id int4 not null)
Hibernate: create table job_metadata (type varchar(31) not null, id int4 not null, name varchar(255), primary key (id))
Hibernate: alter table if exists job_city add constraint FKiksm0d31mc3osxut4ciaf4uof foreign key (job_id) references job
Hibernate: alter table if exists job_city add constraint FKknw4pf63xt1tvnqrmrjrm5hqq foreign key (city_id) references job_metadata
如果我在 City.java 中按照以下注释,那么它会按预期工作,但根据我的“小”研究,此错误已在休眠中修复(因此映射实体不需要使用折旧注释进行注释),或者可能是我错了。
@ManyToMany(mappedBy = "cities")
@org.hibernate.annotations.ForeignKey(name = "none")
private List<Job> jobs;
我使用的环境如下;
- Java11
- 休眠 5.4.28.Final (spring-boot-starter-web)
正如 SimonMartinelli 所指出的,这绝对是一个 Hibernate 错误。对我有用的版本是:
@JoinTable(
name="job_city",
joinColumns = @JoinColumn(name = "job_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT)),
inverseJoinColumns = @JoinColumn(name = "city_id", foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT))
)
我发现当您 (1) 使用 @JoinTable.foreignKey
或 (2) 省略 name
参数时,功能会中断。