[英]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.