【发布时间】:2021-11-12 08:10:10
【问题描述】:
我无法理解如何使用 spring-boot/hibernate 连接 3 个表。 表格是:用户、技术、类别
每个用户都拥有这 10 个类别,但在这些类别中,他们可以保存一项或多项技术。每种技术都可以分为几个不同的类别。
我有一个代码现在部分工作,而不是引用表类别,我只是创建新类别,所以我的 BDD 中有重复项。 理想情况下,我希望每个用户数据结构都像这样(伪代码):
{
{
"category1" : {id, name}
"technologies" [{id, name}, {id, name}, {id, name} ]
},
{
"category2" : {id, name}
"technologies
}
.
.
.
}
我的桌子是:
用户表
public class MyUser {
// other properties
@OneToMany(mappedBy="id")
private Collection<Category> categories;
}
技术
public class Technology {
// other properties
@Id
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
private int id;
@Column(name = "name")
private String name;
}
技术类别:
public class TechnologyCategory {
// other properties
@Id
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
private int id;
@Column(name="name")
private String name;
}
以及我试图将用户与类别联系起来的表格(每个类别都有技术列表)
USER_CATEGORIES
public class UserCategory {
@Id
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
private int id;
@Column(name = "name")
private String technologyCategory; // here I would love to reference technology category table
@ManyToMany()
Collection <Technology> technologies;
}
所以我已经尝试/阅读了这个: Joining three tables using MySQL
ManyToManyToMany - Joining three tables with Hibernate annotations
Hibernate: How to Join three 3 tables in one join table in Annotation?
但没有成功,因为每次尝试实施上述解决方案都会导致我无法解决的异常(所有与休眠相关的无法创建表)。 谢谢
【问题讨论】:
标签: java sql spring-boot hibernate datatables