简体   繁体   English

无法解析方法,使用 JPA 数据的 Spring Boot 应用程序

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

I would like to store a method in a helper class and call that method from another class.我想在辅助类中存储一个方法并从另一个类调用该方法。 The method also fetches data from a jpa repository.该方法还从 jpa 存储库中获取数据。

For some reason when i call the method from the helper class, i get an error : Cannot resolve method 'getDocumentListByProduit' in 'DocumentHelper'.出于某种原因,当我从帮助程序类调用该方法时,出现错误:无法解析“DocumentHelper”中的方法“getDocumentListByProduit”。 The method name doesn't show in IDE's autocompletion either.方法名称也不会显示在 IDE 的自动完成中。 It's like the method isn't mapped for some reason.就像由于某种原因没有映射该方法。 Any hints why?任何提示为什么? thanks in advance.提前致谢。

class from where i wish to call the method:我希望从哪里调用方法的类:

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

the helper class :助手类:



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


then the Repository:然后是存储库:


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 doesn't use the Spring container to instantiate its entities, so Spring does not inject dependencies into entities by default. JPA 不使用 Spring 容器来实例化其实体,因此 Spring 默认不会将依赖项注入到实体中。

You can inject dependencies into objects not managed by the Spring container using @Configurable as described here .你可以依赖注入不使用Spring容器管理对象@Configurable描述这里 This approach requires configuring AspectJ into the project.这种方法需要将 AspectJ 配置到项目中。

Another way would be injecting dependencies manually after JPA constructed an entity using AutowireCapableBeanFactory#autowireBean .另一种方法是在 JPA 使用AutowireCapableBeanFactory#autowireBean构建实体后手动注入依赖项。 This approach may be considered as a bad practice because of repetitiveness if you need it more than in one case.如果您需要不止一种情况,这种方法可能会被认为是一种不好的做法,因为它具有重复性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加 Jpa maven 依赖后无法运行 spring boot app - Cannot run spring boot app after adding Jpa maven dependency Spring 引导 - Jwts.builder 无法解析方法“signWith()” - Spring Boot - Jwts.builder Cannot resolve method 'signWith()’ Spring Boot Data JPA 无法在 MockMVC 测试中自动装配存储库接口 - Spring Boot Data JPA cannot autowire repository interface in MockMVC test Spring Data JPA在Spring Boot Application中没有使用AttributeConverter - Spring Data JPA not using AttributeConverter in Spring Boot Application 使用 spring boot 和 spring 数据 JPA 的 CriteriaQuery 中的空指针异常 - Null pointer exception in CriteriaQuery using spring boot and spring data JPA Spring数据jpa存储库注入失败,在Spring启动时使用@Autowired - spring data jpa repository inject fails using @Autowired in spring boot Spring boot 应用程序中的多个 spring 数据 jpa 模块(非 spring boot)依赖项? - Multiple spring data jpa modules(non spring boot) dependencies in a Spring boot app? 在Spring Boot应用程序中使用Spring Data JDBC - Using Spring Data JDBC in a Spring Boot app Spring Data JPA/Boot: findBy ... 或 - Spring Data JPA/Boot: findBy ... or Spring Boot无法使用Gradle找到spring-boot-starter-dao-jpa软件包? - Spring Boot cannot find spring-boot-starter-dao-jpa package using Gradle?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM