如何解决Spring boot JPA - 使用额外的列查询多对多
我在我的项目中使用 Spring Boot 作为后端。在数据库(MysqL)中,我有一个多对多的关系。实体是:
兴趣实体
@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
}
]
我尝试了什么
方法 findBy
我已经在存储库中尝试过这种方法:
public interface InterestRepository extends CrudRepository<Interest,Long> {
....
List<Interest> findByPriority_user(User user);
}
但是返回一个包含 name_interest 和 interest_id 的数组
谢谢
解决方法
我和你有完全相同的问题,但没有答案。 我有一个桥接表和一个名为 active 的额外列。 我想查询桥表中 active = 1 的数据结果,但从不工作... 在这里寻找答案!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。