这是我在 Controller 下面的代码,即使我传递了一个有效的 key ,结果我总是得到 null。有人可以告诉我为什么会发生这种情况。下面是我的存储库、 Controller 和域类
public interface AbcRepository extends JpaRepository<ForgotPasswordToken, int> {
Abc findByHashKey(String hashKey);
Abc findByUser(User user);
}
映射:
@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
@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();
}
}
最佳答案
存储库和 ABC 类中有不同类型的主键。
你应该改变
JpaRepository<ForgotPasswordToken, int>
至
JpaRepository<ForgotPasswordToken, Long>
并在 ABC 类中使用Long id
private Long id;
并且建议使用对象类型作为 JPA @Id 而不是基元。因此将 long
更改为 Long
。
关于java - spring boot jpa存储库接口(interface)始终返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44626322/