调用空的Spring Boot Projection空结果

问题描述 投票:0回答:1

我有这个 Spring Boot Data JPA 存储库:

  @Query(value = """
                    SELECT .......                                                                                                           
            """, nativeQuery = true)
  ProfileView findByCustomerId(......);

投影:

public interface ProfileView {

    String getRreference();
}

查询:

final ProfileView result = profileRepository.findByCustomerId(......);
final String profile = result.getReference();

我收到错误:

java.lang.NullPointerException: Cannot invoke "com.ProfileView.getReference()" because "result" is null

我的 SQL 查询结果为空。你知道我该如何解决这个问题吗?我需要一个没有 NPE 错误的空

String profile

java spring spring-data-jpa
1个回答
0
投票

使用可选

@Query(value = """
                SELECT .......                                                                                                           
        """, nativeQuery = true)
Optional<ProfileView> findByCustomerId(......);
© www.soinside.com 2019 - 2024. All rights reserved.