Spring Data JPA:查询多对多 [英] Spring Data JPA: query ManyToMany

查看:37
本文介绍了Spring Data JPA:查询多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体 UserTest

@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)

这是使用 Spring Data JPA 的属性表达式特性.签名 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:查询多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆