简体   繁体   English

JPA @Query与实体一样的参数

[英]JPA @Query with Entity like parameter

I am using a @Query with JPA in my Repository. 我在存储库@Query与JPA一起使用。 But, when I execute a program which uses this @Query, I am getting this error : 但是,当我执行使用此@Query的程序时,出现此错误:

java.lang.IllegalStateException: Failed to load ApplicationContext

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: Factory method 'requestMappingHandlerAdapter' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcConversionService' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.format.support.FormattingConversionService]: Factory method 'mvcConversionService' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'supplierRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract XXX.Supplier XXXXX.SupplierRepository.findByNumber(XXXXX.Supplier)!

This is the code with the query : 这是查询的代码:

@Query("SELECT s FROM Supplier WHERE s.number= :#{#supplier.number}")
Supplier findByNumber(@Param("supplier") Supplier supplier);

Why do I have this error? 为什么会出现此错误?

You need to provide alias for entity as below: 您需要为实体提供别名,如下所示:

@Query("SELECT s FROM Supplier s WHERE s.number= :#{#supplier.number}")
Supplier findByNumber(@Param("supplier") Supplier supplier);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM