@Query注释不是读取本机sql select * spring数据

时间:2016-07-19 10:03:28

标签: java spring spring-data-jpa

public interface ConsumerRepository extends CrudRepository<Consumer, Long>  {

    List<Consumer> findByLastName(String lastName);
    List<Consumer> findByFirstName(String firstName);

    //List<Consumer> readExistsFirstName(String firstName);
    //List<Consumer> getByFirstNameAndLastNameOrderByFirstName();
    //Collection<Consumer> findById(Long id);
    @Query("select c from Consumer as c where c.firstName like '%?1%'")
    Collection<Consumer> findByNameContains(String word);

    @Query(value = "SELECT ppc_consumer FROM ppc_consumer WHERE lastname = ?1",
            countQuery = "SELECT count(*) FROM ppc_consumer WHERE lastname = ?1",
            nativeQuery = true)
    Page<Consumer> findByLastname(String lastname, Pageable pageable);
 }

但显示错误“Distinc expected”,得到了*因为我在@Query注释中使用了*?有什么不对吗?

2 个答案:

答案 0 :(得分:1)

你不能在春季jpa中使用*你必须使用别名。试试这个:

@Query(value = "SELECT ppc FROM ppc_consumer ppc WHERE ppc.lastname = ?1",
            countQuery = "SELECT count(ppc) FROM ppc_consumer ppc WHERE ppc.lastname = ?1")

有关详细信息,请参阅此问题

Does Spring Data JPA have any way to count entites using method name resolving?

答案 1 :(得分:1)

让我们按照以下示例进行操作 -

@Query(value = "SELECT * FROM consumer WHERE lastname = :lastName  LIMIT :maxRecord", nativeQuery = true)
StickerProduct findWalletProduct(@Param("lastName") String lastName, @Param("maxRecord") Long maxRecord);

另一种方法,而不是*:

@Query(value = "SELECT c FROM consumer c WHERE lastname = :lastName  LIMIT :maxRecord", nativeQuery = true)
StickerProduct findWalletProduct(@Param("lastName") String lastName, @Param("maxRecord") Long maxRecord);

我希望它有效。感谢