I'm developing Spring boot project, using JPA.
What I wanna know is repository.findById(id) method returns null, whereas data is available in the DB.
Functions save()
and findAll()
are working fine. When I ran the same code on junit test environment, but it completely worked. If the data is hard coded, like memberRepository.findById("M001");
, it working fine.
@Entity
@Table(name="df_member")
public class DfMember {
@Column(name="member_type")
private String memberType;
@Id
@Column(name="id")
private String id;
...columns...
...Getters/Setters.....
@ResponseBody
@RequestMapping(value="/checkIdDuplicate", method=RequestMethod.POST)
public boolean checkIdDuplicate(@RequestBody String id) {
return memberService.isExistByUserId(id);
}
public boolean isExistByUserId(String id) {
Optional<DfMember> member = memberRepository.findById(id);
return member.isPresent();
}
public interface MemberRepository extends CrudRepository<DfMember, String> {
}
Should return Member Object but it's null.
Its findById method retrieves an entity by its id. The return value is Optional<T> . Optional<T> is a container object which may or may not contain a non-null value. If a value is present, isPresent returns true and get returns the value.
Spring JPA Query returns Null instead of List.
As a consequence, findById() returns the actual object and getById returns a reference of the entity.
Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.
While the OP has solved his issue, I have to add my answer to the same problem, but with a different cause, because I'm sure will be helpful after hours of debugging I found in the source code of Hibernate (5.4.18) a try/catch that when a EntityNotFoundException
is thrown in the hydration process the findBy returns null, even if the entity exists, and it's hydrated successfully. This is because a related referenced entity doesn't exists and Hibernate expect it to exists
For example I have two entities Unit and Improvement where I store an unit with id 5 to have an improvement with id 0
(which doesn't exists), then unitRepository.findById()
returns null, instead of the Unit entity with id 5.
@Entity
@Table(name = "units")
public class Unit {
@ManyToOne(fetch = FetchType.LAZY)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = "improvement_id")
@Cascade({ CascadeType.MERGE, CascadeType.PERSIST, CascadeType.DELETE })
private Improvement improvement;
}
The reason this happened was because an import script used 0 as value for the improvement_id instead of the original NULL.
Hint: Becareful with disabling Foreign Key checks in import scritps
Best regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With