[JAVA] A story addicted to EntityNotFoundException of getOne of JpaRepository

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

A story addicted to EntityNotFoundException of getOne of JpaRepository
A story addicted to toString () of Interface proxied with JdkDynamicAopProxy
A story addicted to JDBC Template placeholders
A story I was addicted to in Rails validation settings
A story I was addicted to with implicit type conversion of ActiveRecord during unit testing
[Circle CI] A story I was addicted to at Start Building
A story of frustration trying to create a penetration environment on Ubuntu 20.04
A story of connecting to a CentOS 8 server with an old Ansible
A memo that was soberly addicted to the request of multipart / form-data
The story of forgetting to close a file in Java and failing
A story I was addicted to when testing the API using MockMVC
A story that I was addicted to twice with the automatic startup setting of Tomcat 8 on CentOS 8
I was addicted to a simple test of Jedis (Java-> Redis library)
Ability to display a list of products
Make a margin to the left of the TextField
A story that took time to establish a connection
The story of introducing Ajax communication to ruby
Set the time of LocalDateTime to a specific time
The story of raising Spring Boot 1.5 series to 2.1 series
A story about trying to operate JAVA File
The story of adding the latest Node.js to DockerFile
A story that people who did iOS solidly may be addicted to the implementation of Listener when moving to Android
A memorandum because I was addicted to the setting of the Android project of IntelliJ IDEA
A story I was addicted to when getting a key that was automatically tried on MyBatis
I want to call a method of another class
I was addicted to the record of the associated model
A memorandum of addiction to Spring Boot2 x Doma2
The story of migrating from Paperclip to Active Storage
The story of making a reverse proxy with ProxyServlet
A story about trying to get along with Mockito
GetXxxx of ResultSet was addicted to primitive type (Java)
A story about trying hard to decompile JAR files
A story about reducing memory consumption to 1/100 with find_in_batches
[IOS] To allow rotation of only a specific screen
A story about making catkin_make of rosjava compatible offline
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
The story of migrating a stray batch without an owner from EC2 to a Docker environment
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)
[Java improvement case] Experience story of learning Java in 2 months and changing jobs to a programmer
Story from inexperienced x self-study to becoming an engineer of a web-based in-house development company