During this time, I upgraded from springboot 1.5.x to 2.1, but JPA was also updated at that time, so
hogeRepository.findById(id);
The one I was doing got a compile error.
So, I rewrote it to getOne as described below. Springboot1.5.x to 2.1.5 Upgrade [Until the compilation error is resolved]
If there was no value, I checked the returned value for null, but by changing to getOne, EntityNotFoundExeption occurs at the timing of getting the value of the entity, and try ~ catch does not work either. .. What should I do. .. It became like. Apparently, getOne seems to load lazy, loading data when actually accessing it. So, it seems that getOne is not suitable for this purpose, such as what to do when there is no value in the table. If you return null with orElse, it will be the same as before.
Hoge hoge = hoge.Repository.findById(id).orElse(null);
if (hoge == null) {
}
He wrote in detail on the following page how to use it properly. Difference between getOne and findById in Spring Data JPA?
It was a story about replacing it with a dark cloud.
Recommended Posts