[英]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.