Home > Enterprise >  How can I convert this java filter into a PostgreSQL query?
How can I convert this java filter into a PostgreSQL query?

Time:03-10

I have the below java streams function, but I would like to use a PostgreSQL query instead to retrieve the same information, perform the filter and if possible collect to a list. I would like to write a query in the repo layer such as @Query("") but unsure what to write. Table name is pop_responder and column name is pop_registrants.

    public List<PopResponderDao> getAllPopResponders() {
    return popResponderRepository
        .findAll()
        .stream()
        .filter(responderDao -> responderDao.getPopRegistrationDaos().isEmpty())
        .collect(Collectors.toList());
}

PopRegistrationDaos is of type Collection<PopRegistrationDao>. Repository layer uses JPA:

public interface PopResponderRepository extends JpaRepository<PopResponderDao, Long> {}

CodePudding user response:

There is no support for using stream to interact with database as of now.

If you only need to filter your data you can using JPQL queries. A filter is basically a WHERE clause in JPQL.

SELECT p FROM PopResponderDao p where p.popRegistrationDaos IS NOT EMPTY
  • Related