JPA 存储库找不到值
实体->
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
@Entity
public class ConfirmationToken {
@SequenceGenerator(
name = "confirmation_token_sequence",
sequenceName = "confirmation_token_sequence",
allocationSize = 1
)
@Id @GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "confirmation_token_sequence"
)
private Long id;
@Column(nullable = false)
private String token;
@ManyToOne
@JoinColumn(name = "account_id", nullable = false)
private Account account;
public ConfirmationToken(String token, Account account) {
this.token = token;
this.account = account;
}
}
存储库->
@Repository
@Transactional(readOnly = true)
public interface ConfirmationTokenRepository extends
JpaRepository<ConfirmationToken, Long> {
Optional<ConfirmationToken> findByToken(String token);
}
服务1->
public Optional<ConfirmationToken> getToken(String token) {
return confirmationTokenRepository.findByToken(token);
}
服务2->
@Override
public String confirmToken(String token) {
ConfirmationToken confirmationToken = confirmationTokenService.getToken(token)
.orElseThrow(() -> new IllegalStateException("Token not found"));
return "Token Verified";
}
错误-> java.lang.IllegalStateException:找不到令牌
我检查令牌正常在数据库中,但存储库仍然无法获取数据库中的值 我不知道是什么导致了这个问题。
Entity ->
@Getter
@Setter
@NoArgsConstructor
@EqualsAndHashCode
@Entity
public class ConfirmationToken {
@SequenceGenerator(
name = "confirmation_token_sequence",
sequenceName = "confirmation_token_sequence",
allocationSize = 1
)
@Id @GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "confirmation_token_sequence"
)
private Long id;
@Column(nullable = false)
private String token;
@ManyToOne
@JoinColumn(name = "account_id", nullable = false)
private Account account;
public ConfirmationToken(String token, Account account) {
this.token = token;
this.account = account;
}
}
repository ->
@Repository
@Transactional(readOnly = true)
public interface ConfirmationTokenRepository extends
JpaRepository<ConfirmationToken, Long> {
Optional<ConfirmationToken> findByToken(String token);
}
service1 ->
public Optional<ConfirmationToken> getToken(String token) {
return confirmationTokenRepository.findByToken(token);
}
service2 ->
@Override
public String confirmToken(String token) {
ConfirmationToken confirmationToken = confirmationTokenService.getToken(token)
.orElseThrow(() -> new IllegalStateException("Token not found"));
return "Token Verified";
}
error ->
java.lang.IllegalStateException: Token not found
I checked token is in the database normally but repository still can't get the value in the database
I don't know what's causing this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论