Spring 启动 JPA "Validation failed for query" JPQL 错误
Spring Boot JPA "Validation failed for query" error for JPQL
我正在尝试将自定义查询与 jpa 结合使用(不是 nativeQuery,因为我想将结果映射到自定义对象),但我不知道哪里出了问题。
存储库 class 如下所示:
@Repository
public interface ChallengeCompletionRepository extends JpaRepository<ChallengeCompletion, Integer>{
List<ChallengeCompletion> findByUser(int uid);
List<ChallengeCompletion> findByChallenge(int cid);
List<ChallengeCompletion> findByUserAndChallenge(int uid, int cid);
@Query(value = "SELECT new com.some.rly.long.package.name.ChallengeScore(user_id, count(id)) " +
"FROM ChallengeCompletion " +
"WHERE challenge_id = :cid " +
"GROUP BY user_id " +
"ORDER BY count(id) DESC")
List<ChallengeScore> fetchUserScoreForChallenge(@Param("cid") int cid);
}
而模型 class 我希望列出的结果如下所示:
@Data
@NoArgsConstructor
public class ChallengeScore {
public ChallengeScore(UUID userId, int score){
this.score = score;
this.userId = userId;
}
private int score;
private User user;
@JsonIgnore
private UUID userId;
}
这是 ChallengeCompletion 的模型:
@Entity
@Data
@Table(name = "challenge_completions")
public class ChallengeCompletion extends BaseModel{
@ManyToOne
@JsonBackReference
private User user;
@ManyToOne
private Challenge challenge;
@ManyToOne
private Project project;
}
基本模型是:
@MappedSuperclass
@Data
public abstract class BaseModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@CreationTimestamp
private Timestamp createdAt;
@UpdateTimestamp
private Timestamp updatedAt;
}
错误很简单:"Validation failed for query..."。我也觉得有点奇怪,我需要为我想要构造的 class 使用完全限定的名称,..但这可能是因为 class 不是真正的 @Entity
并且不会自动加载。
可以在此处找到完整的堆栈跟踪:https://pastebin.com/MjP0Xgz4
对于上下文,此查询应该做的是:用户可以多次完成挑战。每次完成都在 ChallengeCompletion 中记录为一行。我想获得不同用户(id)的列表以及他们完成特定挑战的频率。
干杯
编辑:多亏了@DN1 的评论,我发现如果您不想提供 Challenge 对象而只想提供一个 id,您确实可以做类似 cc.challenge.id
的事情,我可以让它工作:
@Query(value = "SELECT new com.energiedienst.smartcity.middleware.module.challenge.model.ChallengeScore(cc.user, count(cc.id)) " +
"FROM ChallengeCompletion cc " +
"WHERE cc.challenge.id = :cid " +
"GROUP BY cc.user " +
"ORDER BY count(cc.id) DESC")
List<ChallengeScore> fetchUserScoreForChallenge(@Param("cid") int cid);
ChallengeScore Class 现在看起来像:
@Data
@NoArgsConstructor
public class ChallengeScore {
public ChallengeScore(User user, long score){
this.score = score;
this.user = user;
}
private long score;
private User user;
}
异常的原因是您正在使用 JPQL,而 JPQL 使用 class/field 名称而不是 table/column 名称(SQL 使用的名称)。 user_id
和challenge_id
貌似是列名,需要改成字段名。有关详细信息,请参阅 this guide。
我正在尝试将自定义查询与 jpa 结合使用(不是 nativeQuery,因为我想将结果映射到自定义对象),但我不知道哪里出了问题。
存储库 class 如下所示:
@Repository
public interface ChallengeCompletionRepository extends JpaRepository<ChallengeCompletion, Integer>{
List<ChallengeCompletion> findByUser(int uid);
List<ChallengeCompletion> findByChallenge(int cid);
List<ChallengeCompletion> findByUserAndChallenge(int uid, int cid);
@Query(value = "SELECT new com.some.rly.long.package.name.ChallengeScore(user_id, count(id)) " +
"FROM ChallengeCompletion " +
"WHERE challenge_id = :cid " +
"GROUP BY user_id " +
"ORDER BY count(id) DESC")
List<ChallengeScore> fetchUserScoreForChallenge(@Param("cid") int cid);
}
而模型 class 我希望列出的结果如下所示:
@Data
@NoArgsConstructor
public class ChallengeScore {
public ChallengeScore(UUID userId, int score){
this.score = score;
this.userId = userId;
}
private int score;
private User user;
@JsonIgnore
private UUID userId;
}
这是 ChallengeCompletion 的模型:
@Entity
@Data
@Table(name = "challenge_completions")
public class ChallengeCompletion extends BaseModel{
@ManyToOne
@JsonBackReference
private User user;
@ManyToOne
private Challenge challenge;
@ManyToOne
private Project project;
}
基本模型是:
@MappedSuperclass
@Data
public abstract class BaseModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@CreationTimestamp
private Timestamp createdAt;
@UpdateTimestamp
private Timestamp updatedAt;
}
错误很简单:"Validation failed for query..."。我也觉得有点奇怪,我需要为我想要构造的 class 使用完全限定的名称,..但这可能是因为 class 不是真正的 @Entity
并且不会自动加载。
可以在此处找到完整的堆栈跟踪:https://pastebin.com/MjP0Xgz4
对于上下文,此查询应该做的是:用户可以多次完成挑战。每次完成都在 ChallengeCompletion 中记录为一行。我想获得不同用户(id)的列表以及他们完成特定挑战的频率。
干杯
编辑:多亏了@DN1 的评论,我发现如果您不想提供 Challenge 对象而只想提供一个 id,您确实可以做类似 cc.challenge.id
的事情,我可以让它工作:
@Query(value = "SELECT new com.energiedienst.smartcity.middleware.module.challenge.model.ChallengeScore(cc.user, count(cc.id)) " +
"FROM ChallengeCompletion cc " +
"WHERE cc.challenge.id = :cid " +
"GROUP BY cc.user " +
"ORDER BY count(cc.id) DESC")
List<ChallengeScore> fetchUserScoreForChallenge(@Param("cid") int cid);
ChallengeScore Class 现在看起来像:
@Data
@NoArgsConstructor
public class ChallengeScore {
public ChallengeScore(User user, long score){
this.score = score;
this.user = user;
}
private long score;
private User user;
}
异常的原因是您正在使用 JPQL,而 JPQL 使用 class/field 名称而不是 table/column 名称(SQL 使用的名称)。 user_id
和challenge_id
貌似是列名,需要改成字段名。有关详细信息,请参阅 this guide。