免责声明::这是我的第一个Java项目。边走边学。
背景:我继承了一个旧数据库,可在其上构建新的RESTful API。我们将Elide与Spring Boot结合使用,以提供符合JSON API的服务。
问题:我们拥有的实体之间通过联接表彼此之间以及彼此之间具有多对多关系。考虑一下Followig模式:
CREATE TABLE ALPHA (
ID VARCHAR(255),
NAME VARCHAR(255),
CONSTRAINT PK_ALPHA PRIMARY KEY (ID)
);
CREATE TABLE BRAVO (
ID VARCHAR(255),
NAME VARCHAR(255),
CONSTRAINT PK_BRAVO PRIMARY KEY (ID)
);
CREATE TABLE RELATIONSHIP (
ID INT AUTO_INCREMENT,
FROM_ID VARCHAR(255),
TO_ID VARCHAR(255)
);
其中资源实体的建模如下:
public class Alpha implements Serializable {
private String id;
private String name;
private Set<Alpha> alphas = new HashSet<>();
private Set<Bravo> bravos = new HashSet<>();
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
public String getId() {
return id;
}
public String getName() {
return name;
}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "RELATIONSHIP",
joinColumns = @JoinColumn(name = "FROM_ID"),
inverseJoinColumns = @JoinColumn(name = "TO_ID")
)
public Set<Alpha> getAlphas() {
return alphas;
}
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(
name = "RELATIONSHIP",
joinColumns = @JoinColumn(name = "FROM_ID"),
inverseJoinColumns = @JoinColumn(name = "TO_ID")
)
public Set<Bravo> getBravos() {
return bravos;
}
}
和关系表:
public class Relationship implements Serializable {
private Integer id;
private String fromId;
private String toId;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
@Column(name = "FROM_ID")
public String getFromId() {
return fromId;
}
@Column(name = "TO_ID")
public String getToId() {
return toId;
}
}
现在,我们有一个Alpha
记录A1
,它与A2
,A3
,B1
和B2
有关系。首先,我们删除与A2
的关系。
根据我们的API,这将是DELETE
与http://localhost:9000/api/alphas/a1/relationships/alphas
的BODY的请求
{
"data": [
{
"type": "alphas",
"id": "a2"
}
]
}
Hibernates在后台执行我所期望的操作,并生成以下SQL查询:
2018-07-13 09:48:23.687 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alpha0_.id as id1_0_,
alpha0_.name as name2_0_
from
alpha alpha0_
where
alpha0_.id in (
?
)
2018-07-13 09:48:23.688 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.690 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alphas0_.from_id as from_id2_2_0_,
alphas0_.to_id as to_id3_2_0_,
alpha1_.id as id1_0_1_,
alpha1_.name as name2_0_1_
from
relationship alphas0_
inner join
alpha alpha1_
on alphas0_.to_id=alpha1_.id
where
alphas0_.from_id=?
2018-07-13 09:48:23.690 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.699 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
select
alpha0_.id as id1_0_,
alpha0_.name as name2_0_
from
alpha alpha0_
where
alpha0_.id in (
?
)
2018-07-13 09:48:23.699 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a2]
2018-07-13 09:48:23.721 DEBUG 7964 --- [nio-9000-exec-5] org.hibernate.SQL :
Hibernate:
delete
from
relationship
where
from_id=?
and to_id=?
2018-07-13 09:48:23.722 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [VARCHAR] - [a1]
2018-07-13 09:48:23.724 TRACE 7964 --- [nio-9000-exec-5] o.h.type.descriptor.sql.BasicBinder : binding parameter [2] as [VARCHAR] - [a2]
关键是delete from relationship where from_id=? and to_id=?
现在,当尝试删除第二个Alpha
关系A3
时,出现问题了,在该关系中,Hibernate几乎执行完全相同的序列,除了DELETE
查询省略了{{1 }},即
and to_id=?
这会意外删除表中的所有其他Hibernate:
delete
from
relationship
where
from_id=?
关系,即A1
和B1
。
这就是我问题的症结所在。似乎Hibernate仅看到另一个相关的B2
记录,因此决定通过省略Alpha
语句来简化查询。
我可能错过了非常明显的东西!
我还应该指出,我尝试在and to_id
表上使用复合键,但无济于事。
答案 0 :(得分:2)
这是一个不寻常的设计,我怀疑这会混淆Hibernate
。在多个Many-to-many
关系之间共享一个联接表,不是一个好的数据库设计,因为它不能具有任何外键/参照完整性。
第二,Hibernate
管理关系,因此可以控制@JoinTable
,但我不知道它如何处理映射到同一表的多个实体关系。显然,不是很好!
最简单的解决方案(如果可以的话)是拥有2个映射表。一个用于Alpha-Alpha
之间的关系,另一个用于Alpha-Bravo
之间的关系。