-1
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);
 }

But is shows error " Distinc expected " , got * because i have used * inside @Query annotation ? what is wrong please ?

Develop4Life
  • 7,581
  • 8
  • 58
  • 76

2 Answers2

2

You can't use * in spring jpa you have to use aliases. Try this way:

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

See this question for more information

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

Community
  • 1
  • 1
amicoderozer
  • 2,046
  • 6
  • 28
  • 44
2

Let's do it as below example-

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

Another way to do same instead of * :

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

I Hope it work. Thanks

buræquete
  • 14,226
  • 4
  • 44
  • 89
sanjeevjha
  • 1,439
  • 14
  • 18