簡體   English   中英

Spring Data返回List <Object[]>

[英]Spring Data returning List<Object[]>

我有這個存儲庫:

@Repository
public interface ProductRepository extends JpaRepository<Product, Long>{

@Query("SELECT p.textToSearch as text, count(*) as counter FROM Product p GROUP BY text_to_search ORDER BY counter DESC")
List<TopProductDTO> findTopProducts();
}

TopProductDTO類的位置是:

public class TopProductDTO {

public TopProductDTO() {}

private String text;
private Integer counter;

// Getters and Setters are omited
}

但是當我執行代碼時

List<TopProductDTO> topProducts = productRepository.findTopProducts();

它返回一個

List<Object[]> insted a List<TopProductDTO>

就像每列都是列表中對象數組的索引一樣...... Spring Data是否應該將查詢中的'text'和'counter'列與TopProductDTO中的字段綁定?

結果我的Thymeleaf模板中出現了這個錯誤:

00:37:22.659 [http-nio-8080-exec-5] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "topProductDTO.text" (products/top:46)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 14): Property or field 'text' cannot be found on object of type 'java.lang.Object[]' - maybe not public?

我正在使用Spring Boot 1.3.3和Postgres 9.2

嘗試使用DTO的構造函數。

聲明一個新的構造函數

public TopProductDTO(String text, Integer count) {
    this.text = text;
    this.count = count;
}

在您的查詢中使用新的構造函數

@Query("SELECT new TopProductDTO(p.textToSearch, count(id))FROM Product p GROUP BY text_to_search ORDER BY counter DESC")
List<TopProductDTO> findTopProducts();
}

使用班級的完全限定名稱。

Spring JPA 解決Thymeleaf返回Strings而不是List<object><div id="text_translate"><p> 我創建了一個 Thymeleaf 表單,並嘗試將具有多個輸入的字段 map 寫入 OneToMany 實體中的列表。 但是,我不斷收到以下錯誤:</p><pre> default message [Failed to convert property value of type 'java.lang.String' to required type 'project.model.Prop' for property 'propList[0]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'project.model.Prop' for property 'propList[0]': no matching editors or conversion strategy found]</pre><p> 這個網站上的許多其他人都遇到過類似的錯誤,我發現有人<a href="https://stackoverflow.com/questions/51024877/thymeleaf-returning-a-string-array-instead-of-listobject" rel="nofollow noreferrer">在這里</a>遇到了幾乎相同的問題。 但是,對於該帖子,一個答案提供了一種非 JPA 解決方案。 同時,答案說有一個JPA的方式來處理這個問題,但沒有提供額外的細節。</p><p> 任何人都可以解釋如何用 JPA 解決該帖子的情況嗎?</p></div></object>

[英]Spring JPA solution to Thymeleaf returning Strings instead of List<Object>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 在Spring數據返回列表中進行重構<Object[]> Spring數據jpa,jparepository返回字符串列表代替DTO對象 refcursor返回一個包含所有數據列表的對象 Spring JPA 解決Thymeleaf返回Strings而不是List<object><div id="text_translate"><p> 我創建了一個 Thymeleaf 表單,並嘗試將具有多個輸入的字段 map 寫入 OneToMany 實體中的列表。 但是,我不斷收到以下錯誤:</p><pre> default message [Failed to convert property value of type 'java.lang.String' to required type 'project.model.Prop' for property 'propList[0]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'project.model.Prop' for property 'propList[0]': no matching editors or conversion strategy found]</pre><p> 這個網站上的許多其他人都遇到過類似的錯誤,我發現有人<a href="https://stackoverflow.com/questions/51024877/thymeleaf-returning-a-string-array-instead-of-listobject" rel="nofollow noreferrer">在這里</a>遇到了幾乎相同的問題。 但是,對於該帖子,一個答案提供了一種非 JPA 解決方案。 同時,答案說有一個JPA的方式來處理這個問題,但沒有提供額外的細節。</p><p> 任何人都可以解釋如何用 JPA 解決該帖子的情況嗎?</p></div></object> Spring MVC - 使用List返回JSON對象 <Custom> 屬性 彈簧數據為嵌套列表對象查找 Spring 數據 JPA 返回相同 object 的多個實例 Spring數據mongo模板返回時間戳而不是普通對象id 在spring-data-mongodb中的$ last聚合中返回對象 Spring數據JPA findByDate總是返回一個空列表
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM