Home > Software design >  Spring JPQL SELECT CASE WHEN returns Null instead of boolean value
Spring JPQL SELECT CASE WHEN returns Null instead of boolean value

Time:10-26

I'm developping a Java Spring API and i have an issue with JPQL. I wanna get a boolean value but i get Null.

    @Query("
          SELECT
            CASE WHEN TRIM(m.mobile) = TRIM(:mobile) 
              THEN true ELSE false 
          END 
          FROM #{#entityName} m"
    )
    Boolean findMemberByMobile(String mobile);
 
    // result: null
    // springboot: 2.7.3, SGBDR: PostgreSQL

Helps please and thanks

CodePudding user response:

Would be helpful if you include the code of your whole repository class, but here's what I found helpful that might help you as well: FYI, it is using Spring Data JPA Query.

    @Repository
    public interface CompanyRepository extends JpaRepository<Company, Integer> {
    @Query("SELECT CASE WHEN COUNT(c) > 0 THEN true ELSE false END FROM Company c WHERE c.name = :companyName")
    boolean existsByName(@Param("companyName") String companyName);
}

Reference: https://codingexplained.com/coding/java/spring-framework/return-boolean-value-from-spring-data-jpa-query

  • Related