是否可以使用“findAll”为JPARepository返回一个Collection / List of Projections?
例:
@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 findByName(String name);
@Query(value = "SELECT name FROM login", nativeQuery = true)
List findAllLoginProjection();
}
使用@Query它的工作原理!但是不可能使用
List findAll();
因为LoginProjection它没有扩展T(登录).
我在想是否有可能为findAll提供一个“别名”,就像findAllXYZ一样,它与findAll做同样的事情.
使用过滤器也可以,但我不想使用它们:
List findAllByName(String name);
我的主要目标是这样的:
@Repository
public interface LoginRepository extends JpaRepository {
Login findByName(String name);
List findAll();
List findAllLoginProjection();
}
这很简单,“零@Query”