簡體   English   中英

無法解析方法,使用 JPA 數據的 Spring Boot 應用程序

[英]Cannot resolve method, spring boot app using JPA Data

我想在輔助類中存儲一個方法並從另一個類調用該方法。 該方法還從 jpa 存儲庫中獲取數據。

出於某種原因,當我從幫助程序類調用該方法時,出現錯誤:無法解析“DocumentHelper”中的方法“getDocumentListByProduit”。 方法名稱也不會顯示在 IDE 的自動完成中。 就像由於某種原因沒有映射該方法。 任何提示為什么? 提前致謝。

我希望從哪里調用方法的類:

@Entity
@Table(name = "document", schema = "table_name")
public class Document {
   private int id;
   private String url;
   private String type;
   private String titre;
   private String description;

   @Autowired
   private DocumentHelper dh;

...

  public Map<String, List<Document>> getDocumentListByProduit(int id){
       Map<String, List<Document>> ret = dh.getDocumentListByProduit(id);
       return ret;
   }

助手類:



@Component
public class DocumentHelper {

   @Autowired
   private DocumentRepository dr;

   public DocumentHelper() {
   }


   public Map<String, List<Document>> getDocumentListByProduit(int id) {
       Map<String, List<Document>> ret = new HashMap<>();
       List<Document> listImg = new ArrayList<>();
       List<Document> listOther = new ArrayList<>();
       List<Document> dList = new ArrayList<>();
       try {
           dList = dr.getDocumentListByProduit(id);
           for (Document tDoc : dList) {
               if (tDoc.getType().equals("image")) {
                   listImg.add(tDoc);
               } else {
                   listOther.add(tDoc);
               }
           }
           ret.put("imageCollection", listImg);
           ret.put("otherCollection", listOther);
       } catch (Exception e) {
           throw new DAOException("Une erreur est survenue : " + e.getMessage());
       }
       return ret;
   }
}


然后是存儲庫:


public interface DocumentRepository extends JpaRepository<Document, Integer> {

    // in method getDocumentListByProduit in DocumentHelper
    @Query(value = "SELECT * FROM DOCUMENT D, DOCUMENT_PRODUIT DP WHERE D.id = DP.id_document AND DP.id_produit = :id_produit;", nativeQuery = true)
    List<Document> getDocumentListByProduit(@Param("id_produit") int id_produit);

}

JPA 不使用 Spring 容器來實例化其實體,因此 Spring 默認不會將依賴項注入到實體中。

你可以依賴注入不使用Spring容器管理對象@Configurable描述這里 這種方法需要將 AspectJ 配置到項目中。

另一種方法是在 JPA 使用AutowireCapableBeanFactory#autowireBean構建實體后手動注入依賴項。 如果您需要不止一種情況,這種方法可能會被認為是一種不好的做法,因為它具有重復性。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM