简体   繁体   English

Spring JPA 投影 findAll

[英]Spring JPA Projection findAll

Is it possible to use "findAll" for a JPARepository returning a Collection/List of Projections?是否可以将“findAll”用于返回投影集合/列表的 JPARepository? Example:例子:

@Entity
public class Login {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    private Integer id;

    private String name;

    private String pass;

    (...)

} }

public interface LoginProjection {
    public String getName();
}

@Repository
public interface LoginRepository extends JpaRepository<Login, Long> {
    Login findByName(String name);

    @Query(value = "SELECT name FROM login", nativeQuery = true)
    List<LoginProjection> findAllLoginProjection();
}

Using @Query it works!使用@Query 它有效! But it is not possible to use但是无法使用

 List<LoginProjection> findAll();

Because LoginProjection it does not extends T (Login).因为 LoginProjection 它不扩展 T(登录)。

I was thinking if it is possible to give a "alias" for findAll like findAllXYZ that does the same thing as findAll.我在想是否有可能为 findAll 提供一个“别名”,比如 findAllXYZ,它与 findAll 做同样的事情。 Using filters it works too, but I do not want to use them:使用过滤器也可以,但我不想使用它们:

 List<LoginProjection> findAllByName(String name);

My main goal would be something like this:我的主要目标是这样的:

@Repository
public interface LoginRepository extends JpaRepository<Login, Long> {
    Login findByName(String name);

    List<Login> findAll();

    List<LoginProjection> findAllLoginProjection();
}

Which is pretty easy and with "zero @Query"这很简单,并且带有“零@Query”

And add a method to the repository:并向存储库添加一个方法:

List<LoginProjection> findAllProjectedBy();

The method name can be simplified to findBy() to match the documentation example at https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections方法名称可以简化为findBy()以匹配https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections中的文档示例

I assume you're not using Spring Data REST , so @Projection won't help here.我假设您没有使用Spring Data REST ,因此@Projection在这里无济于事。 @pavel-molchanov showed one form of expressing the projection, another form is: @pavel-molchanov 展示了一种表达投影的形式,另一种形式是:

List<LoginProjection> findBy();

If you have more than one projection, you can avoid creating a method in the repository for each projection by using dynamic projections like eg this:如果您有多个投影,则可以通过使用动态投影来避免在存储库中为每个投影创建方法,例如:

<T> List<T> findBy(Class<T> projection);

// usage e.g.
... = findBy(LoginProjection.class);
... = findBy(UserSummaryProjection.class);

if you want to be more flexible you can add to the repository this method如果您想更灵活,可以将此方法添加到存储库

<T> List<T> findBy(Class<T> projection);

and then you can call it with the following然后您可以使用以下命令调用它

List<LoginProjection> rows = loginRepository.findBy(LoginProjection.class);

The advantage of this approach is that you can use all the projections you want using the same query这种方法的优点是您可以使用相同的查询使用您想要的所有投影

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM