繁体   English   中英

Spring Boot JPA Repository接口始终返回null

[英]spring boot jpa repository interface always returns null

即使我传递了一个有效的密钥,这在我的控制器中也仍然在我下面的代码中,因此结果总是为null。有人可以告诉我为什么会这样。以下是我的存储库,控制器和域类

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

有关休眠主键,请参见基元或包装器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM