两个实体之间的多对多关系

ffdz8vbo  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(321)

我有两件东西。
价值:

@Table(name = "random_value")
@Entity(name = "random_value")
public class RandomValue
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected long id;

    @Column(name = "random_value", nullable = false)
    private String value;

    public RandomValue()
    {
    }

    public long getId()
    {
        return id;
    }

    public void setId(long id)
    {
        this.id = id;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}

和实体:

@Table(name = "entity")
@Entity(name = "entity")
public class Entity
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    protected long id;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "entity_value_list", joinColumns = {
            @JoinColumn(referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
            @JoinColumn(referencedColumnName = "id", nullable = false)})
    @Fetch(FetchMode.SELECT)
    private List<RandomValue> values;

    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "hidden_entity_value_list", joinColumns = {
            @JoinColumn(referencedColumnName = "id", nullable = false)}, inverseJoinColumns = {
            @JoinColumn(referencedColumnName = "id", nullable = false)})
    @Fetch(FetchMode.SELECT)
    private List<RandomValue> hiddenValues;

    public Entity()
    {
    }

    public List<RandomValue> getValues()
    {
        return values;
    }

    public void setValues(List<RandomValue> values)
    {
        this.values = values;
    }

    public List<RandomValue> getHiddenValues()
    {
        return values;
    }

    public void setHiddenValues(List<RandomValue> hiddenValues)
    {
        this.hiddenValues = hiddenValues;
    }
}

我还有四张table:
一个值:

CREATE TABLE `random_value`
(
    `id`  INT          NOT NULL AUTO_INCREMENT,
    `random_value` VARCHAR(127) NOT NULL UNIQUE,
    PRIMARY KEY (`id`)
);

一个实体:

CREATE TABLE `entity`
(
    `id`      INT          NOT NULL AUTO_INCREMENT,
    PRIMARY KEY (`id`)
);

两个表有多对多的关系

CREATE TABLE `entity_value_list`
(
    `entity_id` INT NOT NULL,
    `values_id`  INT NOT NULL,
    PRIMARY KEY (`entity_id`, `values_id`),
    CONSTRAINT `entity_id`
        FOREIGN KEY (`entity_id`)
            REFERENCES `entity` (`id`),
    CONSTRAINT `values_id`
        FOREIGN KEY (`values_id`)
            REFERENCES `random_value` (`id`)
);

CREATE TABLE `hidden_entity_value_list`
(
    `entity_id` INT NOT NULL,
    `values_id`  INT NOT NULL,
    PRIMARY KEY (`entity_id`, `values_id`),
    CONSTRAINT `entity_id`
        FOREIGN KEY (`entity_id`)
            REFERENCES `entity` (`id`),
    CONSTRAINT `values_id`
        FOREIGN KEY (`values_id`)
            REFERENCES `random_value` (`id`)
);

当我尝试运行使用hibernate和运行时创建的h2数据库的springboot项目时,我得到一个错误消息。

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "ENTITY_ID" already exists; SQL statement:
CREATE TABLE `hidden_entity_value_list`
(
    `entity_id` INT NOT NULL,
    `values_id`  INT NOT NULL,
    PRIMARY KEY (`entity_id`, `values_id`),
    CONSTRAINT `entity_id`
        FOREIGN KEY (`entity_id`)
            REFERENCES `entity` (`id`),
    CONSTRAINT `values_id`
        FOREIGN KEY (`values_id`)
            REFERENCES `random_value` (`id`)
); [90045-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:576) ~[h2-1.4.200.jar:1.4.200]

据我所知 hidden_entity_value_list 看起来像那些 entity_value_list 它在抱怨。我该怎么做才能让这一切顺利进行呢?

slsn1g29

slsn1g291#

约束名称必须是架构范围内唯一的。
您具有约束名称 entity_id 用于两个表中。因此,只需在这两个名称中的一个上重命名即可。

相关问题