gpt4 book ai didi

java - 找不到 int 类型的属性时间戳!走过的路径 : ScoreCard. 分数

转载 作者:行者123 更新时间:2023-12-02 00:59:17 24 4
gpt4 key购买 nike

在尝试运行 Spring Boot 应用程序时,出现以下错误。

1:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userStatsController'

2:

Error creating bean with name 'scoreCardRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List.microservices.book.gamification.repository.ScoreCardRepository.findByUserIdOrderByScoreTimestampDesc(java.lang.Long)! No property timestamp found for type int! Traversed path: ScoreCard.score.

我使用的是H2数据库。

ScoreCardRepository.java

public interface ScoreCardRepository extends CrudRepository<ScoreCard, Long> {

@Query("SELECT SUM(s.score) FROM microservices.book.gamification.domain.ScoreCard s WHERE s.userId = :userId GROUP BY s.userId")
int getTotalScoreForUser(@Param("userId") final Long userId);

@Query("SELECT NEW microservices.book.gamification.domain.LeaderBoard(s.userId, SUM(s.score))"
+ "FROM microservices.book.gamification.domain.ScoreCard s "
+ "GROUP BY s.userId ORDER BY SUM(s.score) DESC")
List<LeaderBoardRow> findFirst10();

List<ScoreCard> findByUserIdOrderByScoreTimestampDesc(Long userId);
}

ScoreCard.java

@Entity
public class ScoreCard {

public static final int DEFAULT_SCORE = 20;

@Id
@GeneratedValue
@Column(name = "CARD_ID")
private Long cardId;

@Column(name = "USER_ID")
private Long userId;

@Column(name = "ATTEMPT_ID")
private Long attemptId;

@Column(name = "SCORE_TS")
private long scoreTimeStamp;

@Column(name = "SCORE")
private int score;

public ScoreCard() {
}

public ScoreCard(Long userId, Long attemptId) {
this.cardId = null;
this.userId = userId;
this.attemptId = attemptId;
this.scoreTimeStamp = System.currentTimeMillis();
this.score = DEFAULT_SCORE;
}

public Long getCardId() {
return cardId;
}

public Long getUserId() {
return userId;
}

public Long getAttemptId() {
return attemptId;
}

public long getScoreTimeStamp() {
return scoreTimeStamp;
}

public int getScore() {
return score;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((attemptId == null) ? 0 : attemptId.hashCode());
result = prime * result + ((cardId == null) ? 0 : cardId.hashCode());
result = prime * result + score;
result = prime * result + (int) (scoreTimeStamp ^ (scoreTimeStamp >>> 32));
result = prime * result + ((userId == null) ? 0 : userId.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ScoreCard other = (ScoreCard) obj;
if (attemptId == null) {
if (other.attemptId != null)
return false;
} else if (!attemptId.equals(other.attemptId))
return false;
if (cardId == null) {
if (other.cardId != null)
return false;
} else if (!cardId.equals(other.cardId))
return false;
if (score != other.score)
return false;
if (scoreTimeStamp != other.scoreTimeStamp)
return false;
if (userId == null) {
if (other.userId != null)
return false;
} else if (!userId.equals(other.userId))
return false;
return true;
}

@Override
public String toString() {
return "ScoreCard [cardId=" + cardId + ", userId=" + userId + ", attemptId=" + attemptId + ", scoreTimeStamp="
+ scoreTimeStamp + ", score=" + score + "]";
}

}

UserStatsController.java

@RestController
@RequestMapping("/stats")
public class UserStatsController {

private GameService gameService;

@Autowired
public UserStatsController(GameService gameService) {
this.gameService = gameService;
}

@GetMapping
public GameStats getStatsForUser(@RequestParam("userId") Long userId) {
return gameService.retrieveStatsForUser(userId);
}

}

应用程序属性

server.port=8081

spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:h2:file:~/gamification;DB_CLOSE_ON_EXIT=FALSE;
spring.jpa.properties.hibernate.show_sql=true

最佳答案

您应该将findByUserIdOrderByScoreTimestampDesc更改为findByUserIdOrderByScoreTimeStampDesc

说明:实体类的属性必须与默认命名策略匹配(除非专门配置)

我希望这能起作用。

您的实体类的属性名称为 scoreTimeStamp (时间戳单词的大写),但在方法声明中,它是 ScoreTimestamp (时间戳单词的小写) )。

PS:根据您在问题中提供的信息和以下评论,这应该只是问题所在。

关于java - 找不到 int 类型的属性时间戳!走过的路径 : ScoreCard. 分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57783356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com