我在项目中使用Spring Boot作为后端。在数据库(MySQL(中,我有一种多对多的关系。实体包括:
- 兴趣包含字段id、nameInterest和priority
- 用户,包含字段id、电子邮件、年龄、流派、用户名、密码和优先级
- RelUserInterest(中间表(,包含字段user、interest和priority
利益实体
@Id
@GeneratedValue
private long id;
@NotEmpty
@Column(unique = true)
private String nameInterest;
@OneToMany(mappedBy = "interest", cascade = CascadeType.ALL)
@NotNull
Set<RelUserInterest> priority = new HashSet<>();
用户实体
@Id @GeneratedValue private long id;
@NotNull
@Column (unique = true) private String email;
@NotNull private int age;
@NotNull private String genre;
@NotNull private String userName;
@NotNull private String password;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
@NotNull
Set<RelUserInterest> priority = new HashSet<>();
RelUserInterest实体
@Id
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
User user;
@Id
@ManyToOne
@JoinColumn(name = "interest_id", referencedColumnName = "id")
Interest interest;
int priority;
我想要什么
我想要一个返回下一个信息的查询:具有额外列的用户感兴趣的列表(在本例中为priority列(。JSON格式为:
[
{
interest_id: 1,
name_interest: "Museum",
priority: 5
},
{
interest_id: 2,
name_interest: "Cathedral",
priority: 6
}
]
我尝试过的
方法查找
我在存储库中尝试过这种方法:
public interface InterestRepository extends CrudRepository<Interest, Long> {
....
List<Interest> findByPriority_user(User user);
}
但是返回一个带有name_interest和interest_id 的数组
谢谢
我有和你完全相同的问题,但没有答案。我有一个桥接表和一个额外的列,叫做active。我想查询桥接表中active=1的数据结果,但永远不起作用。。。在这里寻找答案!