Spring Data JPA JpaRepository getOne()

In this article, we will see about Spring Data JPA JpaRepository getOne() Example using Spring Boot and Oracle.

The getOne() method is used to retrieves an entity based on id and it is available in JpaRepository interface. The getOne() method has been defined as below.

T getOne(ID id);

Using getOne() method we get a single record(entity) on basis of id. If we provide id null, it will throw error ” The given id must not be null”.

The getOne() throws EntityNotFoundException if no record/entity exists for given id.

Internally getOne() method use EntityManger’s getReference() method(as below).

Note –  The getOne() just returns a reference of the entity. All fields might contain default values.

Let’s see in below code how to use the Spring Data JPA CrudRepository getOne() method for get operation. We will see a complete example later in the post.

First, Define repository interface extending JpaRepository since getOne() defined in JpaRepository interface.

In ServiceImpl class we can use getOne() method as below.

 

The calling getOne() returns a lazily fetched entity. When we say lazily fetched what does it mean?

Student studentResponse = studentRepository.getOne(id);

In the above case(getOne()), studentResponse will contain default values for all fields.

For example, consider we have below entity.

In the above entity, we have id, name, rollNmuber and university fields and also we have corresponding getter and setter. In this scenario lazily fetched means when below code will execute studentResponse reference would have 0 or null values for fields.

Student studentResponse = studentRepository.getOne(id);

In the above screenshot, we can see we have studentResponse reference but all fields have null as a value. So when fields will get initialized? The answer is on-demand i.e in our case while deserialization or when any getter is called.

Hope this makes sense.

 

Let’s see an example of Spring Data JPA JpaRepository getOne() method Example.

Open eclipse and create maven project, Don’t forget to check ‘Create a simple project (skip)’ click on next.  Fill all details(GroupId – springdatagetone, ArtifactId – springdatagetone and name – springdatagetone) and click on finish. Keep packaging as the jar.

Modify pom.xml

Note – In pom.xml we have defined javac.exe path in configuration tag. You need to change accordingly i.e where you have installed JDK.

The directory structure of Spring Data JPA JpaRepository getOne() example.

If you see any error for oracle dependency then follow these steps.

Directory structure –

Spring Data JPA JpaRepository getOne()

 

 

Student.java

StudentController.java

Note – Add @JsonIgnoreProperties(value={“hibernateLazyInitializer”,”handler”,”fieldHandler”}) with entity else you might get below exception.

Type definition error: [simple type, class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

 

Note – See more details about @Controller and RestController here.

StudentRepository.java – interface

StudentService.java – interface

 

StudentServiceImpl.java

Note – See more about @Component, @Controller, @Service and @Repository annotations here.

SpringMain.java

See more details about @ComponentScan here.

JpaConfig.java

 

See more details about @Configuration annotations here.

application.properties

Let’s run the SpringMain class(run as java application).

Perform save operation first using below REST API.

http://localhost:9091/student/save

Request Data –

Response data –

 

 

Get operation.

API -http://localhost:9091/student/{id}

http://localhost:9091/student/1

Spring Data JPA JpaRepository getOne()

 

See brief about Spring Data JPA Repository hierarchy as below.

Spring Data JPA JpaRepository getOne()

 

That’s all about Spring Data JPA JpaRepository getOne() example using Spring Boot and Oracle.

 

You may like.

Other Spring Data JPA and Hibernate tutorials.

Spring Data JPA getOne() docs.

 

Top