JPA @Query与实体一样的参数

时间:2018-12-12 12:46:55

标签: java spring spring-data-jpa

我在存储库中将@Query与JPA一起使用。但是,当我执行使用此@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)!

这是带有查询的代码:

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

为什么会有这个错误?

1 个答案:

答案 0 :(得分:2)

您需要为实体提供别名,如下所示:

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