本文介绍了Spring Data JPA:查询ManyToMany的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有实体用户
和测试
@Entity
public class User {
private Long id;
private String userName;
}
@Entity
public class Test {
private Long id;
@ManyToMany
private Set<User> users;
}
我可以通过用户实体获得所有测试:
I can get all tests by User entity:
public interface TestRepository extends JpaRepository<EventSettings, Long> {
List<Test> findAllByUsers(User user);
}
但是我可以使用哪个查询来查找的userName ?
But which query can I use for finding all tests by userName
?
推荐答案
您希望获得以下方法签名:
The following method signature will get you want to want:
List<Test> findByUsers_UserName(String userName)
这是使用功能。签名 Users_UserName
将被转换为JPQL x.users.userName
。请注意,这将在给定的用户名上执行完全匹配。
This is using the property expression feature of Spring Data JPA. The signature Users_UserName
will be translated to the JPQL x.users.userName
. Note that this will perform an exact match on the given username.
这篇关于Spring Data JPA:查询ManyToMany的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!