【问题标题】:JPA @ForeignKey(value = ConstraintMode.NO_CONSTRAINT) not working with @ManyToManyJPA @ForeignKey(value = ConstraintMode.NO_CONSTRAINT) 不适用于 @ManyToMany
【发布时间】:2021-06-12 22:17:42
【问题描述】:

我有两个具有多对多关系的实体。这里的目标是在应用程序以无外键

开始时创建架构

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;

我使用的环境如下;

  • Java 11
  • 休眠 5.4.28.Final (spring-boot-starter-web)

【问题讨论】:

标签: spring-boot hibernate spring-data-jpa constraints many-to-many


【解决方案1】:

正如 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 参数时,功能会中断。

【讨论】:

  • 我把它改成下面但没有用:(@ManyToMany @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)) // foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT), // inverseForeignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT) )
  • 嗯,很奇怪。我在 5.4.29 上对其进行了测试。如果将@JoinTable.foreignKey@JoinTable.inverseForeignKey 全部删除,它会起作用吗?
  • 我已将@JoinTable.foreignKey@JoinTable.inverseForeignKey 注释掉但没有用。
猜你喜欢
  • 2020-10-31
  • 2015-12-11
  • 2016-07-12
  • 2011-03-30
  • 2017-10-30
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多