两个实体之间的多个多对多关系
Multiple many-to-many relations between two entities
我有两个对象。
值:
@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;
}
}
我也有四张表:
一个代表 值:
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`)
);
当我尝试 运行 我的 spring 引导项目时,它使用休眠和 运行 创建的 h2 数据库,我收到一条错误消息。
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
中的字段,所以它在抱怨。我该如何解决这个问题并使整个事情正常进行?
约束名称在架构范围内必须是唯一的。
您在两个表中都使用了 CONSTRAINT 名称 entity_id
。所以只需重命名两者中的一个即可。
我有两个对象。
值:
@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;
}
}
我也有四张表:
一个代表 值:
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`)
);
当我尝试 运行 我的 spring 引导项目时,它使用休眠和 运行 创建的 h2 数据库,我收到一条错误消息。
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
中的字段,所以它在抱怨。我该如何解决这个问题并使整个事情正常进行?
约束名称在架构范围内必须是唯一的。
您在两个表中都使用了 CONSTRAINT 名称 entity_id
。所以只需重命名两者中的一个即可。