0

this my code below in my controller even though i pass a key which is valid i always get null as a result.Can someone tell me why is this happening .below is my repository ,controller and domain class

public interface AbcRepository extends JpaRepository<ForgotPasswordToken, int> {
    Abc findByHashKey(String hashKey);
    Abc findByUser(User user);
}

Mapping:

@RequestMapping(value = "/validateKey/{hashKey}", method = RequestMethod.GET)
public ResponseEntity validateKey(@PathVariable String hashKey) {
    Abc abc = AbcRepository.findByHashKey(hashKey);
    if (abc == null) {
        return ResponseEntity.badRequest().body("Invalid key");
    }
    return ResponseEntity.ok().body(abc.getUser());
}

Entity:

@Entity
@Data
public class Abc {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id")
    private User user;

    private String hashKey;

    private String email;

    @Temporal(TemporalType.TIMESTAMP)
    private Date createdAt;

    @PrePersist
    public void onCreate() {
        this.createdAt = new Date();
    }

}
ByeBye
  • 6,650
  • 5
  • 30
  • 63
user2452537
  • 43
  • 1
  • 9

1 Answers1

1

You have different types of primary key in the repository and ABC class.

You should change

JpaRepository<ForgotPasswordToken, int> 

to

JpaRepository<ForgotPasswordToken, Long>

And use Long id in the ABC class

private Long id;

And it is recommended to use object types as JPA @Id instead of primitives. So change long to Long.

See Primitive or wrapper for hibernate primary keys

Mike Adamenko
  • 2,944
  • 1
  • 15
  • 28