i am currently making a website using spring and i stumble upon this basic scenario that i don\'t have any idea on how to solve this specific code: Entity = Optional;
The answers lack some job to do. Before you call get()
, you should do some checking with isPresent()
. Like so:
Optional optionalEntity = roomRepository.findById(roomId);
if (optionalEntity.isPresent()) {
RoomEntity roomEntity = optionalEntity.get();
...
}
Read this great article about optionals: https://dzone.com/articles/using-optional-correctly-is-not-optional