2017-06-19 89 views
0

这是我的代码在我的控制器下面,即使我传递一个有效的密钥我总是得到null作为结果。可以有人告诉我为什么会发生这种情况。下面是我的存储库,控制器和域类春季启动jpa存储库接口总是返回空

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(); 
    } 

} 
+0

你的版本库返回'ForgotPasswordToken'不'Abc',你关键是'long'不'int' – ByeBye

+0

或者,你没有任何实体符合您的条件 – ByeBye

+0

嗨,我试着用长期仍然得到相同的结果。 Im使用ForgotPasswordToken而不是Abc.Sorry在复制代码时犯了一个错误,希望使用别名为ForgotPasswordToken,因此使用Abc – user2452537

回答

1

你在存储库和ABC类中有不同类型的主键。

你应该改变

JpaRepository<ForgotPasswordToken, int> 

JpaRepository<ForgotPasswordToken, Long> 

而且在ABC类

private Long id; 

使用Long id,建议使用对象类型作为JPA @Id代替原语。因此请将long更改为Long

Primitive or wrapper for hibernate primary keys