2017-07-31 211 views
0

我使用springboot創建實體並創建CRudRepository的CRUD函數,我可以使用@Service創建findbyHoliday(String name) 我真的想要使用springboot CrudRepository,任何人都可以給我一些方向?spring boot CrudRepository findby函數無法映射

這裏我

application.properties

spring.jpa.generate-ddl=true 
spring.jpa.hibernate.ddl-auto=update 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 
spring.jpa.hibernate.use-new-id-generator-mappings=true 
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl 
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
spring.jpa.properties.hibernate.format_sql = false 
spring.jpa.properties.hibernate.default_schema=SPRINGUSER 

我敢嘗試創建一個功能擴展CrudRepository和我的繼承人接口

HolidayScheduleRepository.java

public interface HolidayScheduleRepository extends CrudRepository<HolidaySchedule,Long>{ 
List<HolidaySchedule> findByHDay(@Param("HDay") String hday); 

} 
所示

下面有我的實體

HolidaySchedule.java

@Entity // This tells Hibernate to make a table out of this class 
@Table(name="TWD_HSD_HOLIDAY_SCHEDULE") 
public class HolidaySchedule { 
    @Id 
    @Column(name="HSD_ID") 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Integer Id; 
    @Column(name="HSD_DATE") 
    private String HDate; 
    @Column(name="HSD_DAY") 
    private String HDay; 
    @Column(name="HSD_HOLIDAY") 
    private String HHoliday; 

最後我控制器方法

@Controller // This means that this class is a Controller 
@RequestMapping(path="/test") 
....... 
@GetMapping(path="crudtest") 
     @ResponseBody 
     public List<HolidaySchedule> findHolidayByName(){ 
      return holidayScheduleRepository.findByHDay("Monday"); 
     } 

ERROR 81372

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'holidayScheduleController': Unsatisfied dependency expressed through field 'holidayScheduleRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'holidayScheduleRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.util.List com.twd.holidayschedule.HolidayScheduleRepository.findByHDay(java.lang.String)! 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:358) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1337) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:574) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:491) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:865) ~[spring-context-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:809) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:404) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:347) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1263) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1252) [spring-boot-2.0.0.BUILD-SNAPSHOT.jar:2.0.0.BUILD-SNAPSHOT] 
    at com.twd.TradingWatchDogApplication.main(TradingWatchDogApplication.java:14) [classes/:na] 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'holidayScheduleRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.util.List com.twd.holidayschedule.HolidayScheduleRepository.findByHDay(java.lang.String)! 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1701) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:491) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:250) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:570) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    ... 19 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.util.List com.twd.holidayschedule.HolidayScheduleRepository.findByHDay(java.lang.String)! 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:106) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:214) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:77) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$null$0(RepositoryFactorySupport.java:410) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[na:1.8.0_131] 
    at java.util.Iterator.forEachRemaining(Iterator.java:116) ~[na:1.8.0_131] 
    at java.util.Collections$UnmodifiableCollection$1.forEachRemaining(Collections.java:1049) ~[na:1.8.0_131] 
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[na:1.8.0_131] 
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[na:1.8.0_131] 
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[na:1.8.0_131] 
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[na:1.8.0_131] 
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[na:1.8.0_131] 
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[na:1.8.0_131] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$new$2(RepositoryFactorySupport.java:412) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at java.util.Optional.map(Optional.java:215) ~[na:1.8.0_131] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:403) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:241) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$2(RepositoryFactoryBeanSupport.java:258) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.util.Lazy.get(Lazy.java:62) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:261) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1760) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1697) ~[spring-beans-5.0.0.BUILD-SNAPSHOT.jar:5.0.0.BUILD-SNAPSHOT] 
    ... 29 common frames omitted 
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [hDay] on this ManagedType [com.twd.holidayschedule.HolidaySchedule] 
    at org.hibernate.metamodel.internal.AbstractManagedType.checkNotNull(AbstractManagedType.java:128) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final] 
    at org.hibernate.metamodel.internal.AbstractManagedType.getAttribute(AbstractManagedType.java:113) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final] 
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:569) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:377) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:300) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:205) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:117) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:54) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:111) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:90) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:78) ~[spring-data-commons-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:129) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:257) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:72) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-2.0.0.BUILD-SNAPSHOT.jar:na] 
    ... 51 common frames omitted 
+0

'@ Query'需要JPQL查詢默認不是SQL查詢。您正試圖編寫SQL查詢而不是JPQL查詢。你也試圖選擇一個列,但期望一個完整的對象(這也不會起作用)。還有什麼是你用任何一種方法得到的錯誤。 –

+0

不要將其他信息添加爲註釋。編輯你的問題,並做適當的格式。 –

回答

1

這是一個棘手的問題,我強烈建議您遵守標準Java約定,例如用小寫字母(駝峯格式)開始所有字段名稱。我相信這是它無法找到該領域的原因。

你會希望它看起來像這樣在Repository

`List<HolidaySchedule> findByHDay(String hDay);` 

@Column(name="HSD_DAY") private String hDay;

模型中的

從此開始,看看它在哪裏得到你。

請參閱以下Spring參考

https://docs.spring.io/spring-data/jpa/docs/1.11.6.RELEASE/reference/html/#jpa.query-methods.query-creation

+0

是的,我試過了,它也可以工作,但我已經構建了很多實體和數據庫,並且我創建了很多擴展存儲庫作爲@Service – user2285201

+0

如果您不需要,您不需要使用JPQL ...它可以爲您自動生成該查詢,只需在接口中如何命名該方法即可。 – Chad

+0

我真的很想在我的數據庫中保存這個命名爲「HST_XXX」的任何解決方案嗎? – user2285201

1

還有一個解決辦法是使用原生查詢,但更好的解決方案是由乍得建議

List<HolidaySchedule> findByHDay(String hDay);` 
下面

針對此問題的原生查詢

public interface HolidayScheduleRepository extends CrudRepository<HolidaySchedule,Long>{ 

    @Query("SELECT * FROM TWD_HSD_HOLIDAY_SCHEDULE WHERE HSD_DAY = :hday") 
    List<HolidaySchedule> findByHDay(@Param("hday) String hday); 

}