[英]spring boot jpa repository interface always returns null
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 即使我传递了一个有效的密钥,这在我的控制器中也仍然在我下面的代码中,因此结果总是为null。有人可以告诉我为什么会这样。以下是我的存储库,控制器和域类
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();
}
}
You have different types of primary key in the repository and ABC class. 您在存储库和ABC类中具有不同类型的主键。
You should change 你应该改变
JpaRepository<ForgotPasswordToken, int>
to 至
JpaRepository<ForgotPasswordToken, Long>
And use Long id
in the ABC class 并在ABC类中使用
Long id
private Long id;
And it is recommended to use object types as JPA @Id instead of primitives. 并且建议将对象类型用作JPA @Id而不是基元。 So change
long
to Long
. 因此,将
long
更改为Long
。
See Primitive or wrapper for hibernate primary keys 有关休眠主键,请参见基元或包装器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.