JPA Repository.findById() returns null but the value is exist on db

Jin-guk Park :

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

@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.....

Controller

    @ResponseBody
    @RequestMapping(value="/checkIdDuplicate", method=RequestMethod.POST)
    public boolean checkIdDuplicate(@RequestBody String id) {

       return memberService.isExistByUserId(id);
    }

MemberService

    public boolean isExistByUserId(String id) {
        Optional<DfMember> member = memberRepository.findById(id);
        return member.isPresent();
    }

Repository

public interface MemberRepository extends CrudRepository<DfMember, String> {

}

Should return Member Object but it's null.

Alexpandiyan Chokkan :

You have to change @RequestBody to @RequestParam. Please update your controller code as below.

    @ResponseBody
    @RequestMapping(value="/checkIdDuplicate", method=RequestMethod.POST)
    public boolean checkIdDuplicate(@RequestParam String id) {

       return memberService.isExistByUserId(id);
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=127852&siteId=1