我在理解如何使用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?
但是没有成功,因为每次尝试实现上面的解决方案都会导致异常(都与hibernate连接,无法创建表),这是我无法解决的。谢谢
发布于 2021-09-17 13:05:20
如果我没有理解错的话,假设你的示例代码可以工作,那么下面的代码就可以为你工作了:
User类,它加入了以下类别:
public class User {
@Id
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
private int id;
// other properties
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "user_categories", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"))
private Set<Category> categories;
}类别实体,其中包含技术的类别和技术:
public class Category {
@Id
@SequenceGenerator(name = "id_seq", sequenceName = "id_seq", allocationSize = 1, initialValue = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_seq")
private int id;
// other properties
@ManyToOne
@JoinColumn(name = "technology_category_id")
private TechnologyCategory category;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "category_technologies", joinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "technology_id", referencedColumnName = "id"))
private Set<Technology> technologies;
}TechnologyCategory实体:
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;
}技术实体:
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;
}如果不希望id出现在JSON中,只需将@JsonIgnore注释放在id属性之上即可。
https://stackoverflow.com/questions/69223569
复制相似问题