2017-09-14 68 views
0

我試圖使用jpa自定義查詢(不是nativeQuery,因爲我想將結果映射到自定義對象),我不知道什麼是錯的。春季啓動JPA「驗證失敗的查詢」JPQL的錯誤

repository類看起來是這樣的:

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

和模型類我想resultl是在外觀還是列出這樣的:

@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; 
} 

錯誤很簡單:「驗證失敗,查詢...」。我也覺得有點奇怪,我需要使用我想構建的類的全限定名,但是這可能是由於類不是真實的@Entity而且不會自動加載。

一個完整的堆棧跟蹤可以在這裏找到:https://pastebin.com/MjP0Xgz4

對於背景下,這是什麼查詢應該做的是:用戶可以多次完成challanges。在ChallengeCompletion中,每個完成都記錄爲一行。我想獲得不同用戶(ID)的列表以及他們多久完成一次挑戰。

乾杯

編輯:多虧了@評論DN1我能得到它找出你的確可以這樣做cc.challenge.id如果你不想給一個挑戰對象,而是隻有一個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類現在看起來像:

@Data 
@NoArgsConstructor 
public class ChallengeScore { 

    public ChallengeScore(User user, long score){ 
     this.score = score; 
     this.user = user; 
    } 

    private long score; 
    private User user; 

} 
+0

您是否嘗試過使用預測(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections)或編寫自定義實現 – pvpkiran

+0

@pvpkiran,感謝您的答案,不確定您的定製實施意味着什麼。據我所知,上述情況應該起作用。我確實有這個問題,我經常需要一箇中介類來進行自定義查詢,所以我想了解這個問題而不是解決它。錯誤表示查詢有合成錯誤。 – Tom

+0

你可以把整個堆棧strace。投影是專門用於中間類的情況。我的意思是你可以寫一個自定義存儲庫,並將'ChallengeCompletion'轉換爲'ChallengeScore' https://stackoverflow.com/questions/43265331/propertyreferenceexception-in-custom-repository/43273248#43273248 – pvpkiran

回答

1

的原因例外是,您正在使用JPQL和JPQLü ses類/字段名稱而不是表/列名稱(SQL使用什麼)。表面上user_idchallenge_id是列名稱,您需要將它們更改爲使用字段名稱。有關詳細信息,請參閱this guide