简体   繁体   English

Spring Data JPA findById()方法返回null而不是Empty可选

[英]Spring Data JPA findById() method returning null instead of Empty Optional

I have a method that is making use of Spring Data JPA's findById() method is supposed to return an Optional. 我有一个利用Spring Data JPA的findById()方法的方法应该返回Optional。 However, in case the entity is not found by the specified id, it is returning null instead of an Empty Optional. 但是,如果指定的ID未找到该实体,则它将返回null而不是Empty Optional。

 public TicketEntity findTicket(String ticket) throws EntityNotFoundException {

    Optional<TicketEntity> op = ticketEntityRepository.findById(ticket);

    TicketEntity ticketEntity = op.orElseThrow(() -> new EntityNotFoundException("ticket with the id " + ticket + " not found in the system"));

    return ticketEntity;
}

While debugging, I found that the value of op is null. 调试时,我发现op的值为null。 This is the piece of code that is failing. 这是一段失败的代码。 I am using Spring Data JPA 2.0.8.RELEASE. 我正在使用Spring Data JPA 2.0.8.RELEASE。 Please help 请帮忙

In a comment you state that this is in a test with mocked dependencies. 在注释中,您声明这是在具有模拟依赖项的测试中。 The mocking takes Spring Data JPA out of the picture completely as it now is just a proxy implemented by a mock from Mockito . 该模拟完全使Spring Data JPA脱颖而出,因为它现在只是由Mockito的模拟实现的代理。

The default behavior for a mock is to return null . 模拟的默认行为是返回null

By default, for all methods that return a value, a mock will return either null, a primitive/primitive wrapper value, or an empty collection, as appropriate. 默认情况下,对于所有返回值的方法,模拟将酌情返回null,原始/原始包装器值或空集合。 For example 0 for an int/Integer and false for a boolean/Boolean. 例如,对于int / Integer为0,对于boolean / Boolean为false。

As you are running with a mock you will need to instruct it to return an Optional.empty() else you will get null . 当您运行模拟程序时,您将需要指示它返回Optional.empty()否则您将得到null

NOTE: You might want to create an improvement request for Mockito to default return Optional.empty in the case of an Optional return type. 注意:您可能要创建一个改进请求到的Mockito默认返回Optional.empty在的情况下, Optional返回类型。

What is the implementation of your Repository Class? 您的存储库类的实现是什么? The following repository and test case works for me. 以下存储库和测试用例对我有用。

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonRepoTest {

    @Autowired
    private PersonRepository personRepository;

    @Test
    public void testFindById(){
        Optional<Person> byId = personRepository.findById(1);
        Assert.assertTrue(byId != null);
    }
}

public interface PersonRepository  extends CrudRepository<Person, Integer> {
}

The line of code 代码行

Optional op = ticketEntityRepository.findById(ticket); 可选op = ticketEntityRepository.findById(ticket);

return list of result set if data exists in the system, otherwise it is obvious that it will return null instead of empty optional. 如果系统中存在数据,则返回结果集列表,否则很明显它将返回null而不是空的optional。 If you need an empty optional list you can twist it as follows 如果您需要一个空的可选列表,可以按照以下方式进行扭曲

List op = ticketEntityRepository.findById(ticket).orElse(new ArrayList()); 列表op = ticketEntityRepository.findById(ticket).orElse(new ArrayList());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Data JPA:如何在CrudRepository的findById方法中修复NullPointerException? - Spring Data JPA: How to fix NullPointerException in findById method of CrudRepository? Spring Data JPA findById()抛出ClassCastException - Spring Data JPA findById() throwing ClassCastException spring-data-jpa 多对多 findByID - spring-data-jpa many to many findByID 为什么 spring 数据 redis findById 在升级到版本 2.3.2.RELEASE 后在 Optional 中返回 null 值 - Why spring data redis findById returns null values in Optional after upgrading to version 2.3.2.RELEASE 我可以通过 ID 列表而不是 Spring 数据 JPA 中的经典 findById() 找到吗? - Can I find by a list of IDs instead the classic findById() in Spring Data JPA? Spring Data MongoDb findById 不返回结果 - Spring Data MongoDb findById not returning results Spring 中显式支持 null 的查询方法中的可选参数 JPA - Optional parameter in query method with explicit null support in Spring JPA Spring Boot JPA 不使用 findById 返回现有结果 - Spring boot JPA no returning existing result using findById Spring Data JPA 如何使用 Kotlin nulls 而不是 Optional - Spring Data JPA How to use Kotlin nulls instead of Optional Spring 数据 JPA findAll() 或 findbyId() 在 spring 引导中返回错误值 - Spring Data JPA findAll() or findbyId() return wrong value in spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM