jpa存储库有2种相同的方法

时间:2015-08-07 07:53:47

标签: java hibernate jpa

我正在尝试在JPA Repository中添加两个相同的方法名称。我不确定是否可能。

这就是我在JPA界面中实际尝试做的事情:

public interface TbiDDCustomQueryConfigDao extends CrudRepository<TbiDDCustomQueryConfig, Integer>, JpaRepository<TbiDDCustomQueryConfig, Integer>{

    @Query("select c from TbiDDCustomQueryConfig c, TbiDataSource d where c.datasourceId= d.dataSourceId and c.tbiDDConfigMaster.ddConfigId = ?1 and c.isActive = ?2 and d.isActive='Y'")
    List<TbiDDCustomQueryConfig> findByDdConfigIdAndIsActive(Integer ddConfigId,String isActive);

    TbiDDCustomQueryConfig findByDdConfigIdAndIsActive(Integer ddConfigId,String isActive);

}  

编译错误告诉我重命名该方法。我改变了第二种方法如下:

TbiDDCustomQueryConfig findByDdConfigIdAndIsActive1(Integer ddConfigId,String isActive);  

但是当我运行代码时,我收到了错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.acinfotech.timebound.jpa.dao.TbiDDCustomQueryConfigDao com.acinfotech.timebound.jpa.service.ReportJobsPersistenceServiceImpl.tbiDDCustomQueryDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tbiDDCustomQueryConfigDao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property dd found for type TbiDDCustomQueryConfig!
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
    ... 48 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tbiDDCustomQueryConfigDao': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property dd found for type TbiDDCustomQueryConfig!
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1512)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489)
    ... 50 more
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property dd found for type TbiDDCustomQueryConfig!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:359)
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:201)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:291)
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:271)
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:83)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:84)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1571)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1509)
    ... 60 more

2 个答案:

答案 0 :(得分:1)

您不能使用相同的名称使用相同的参数。 Java不允许这样做。如果重命名由spring-data-jpa自动解释的lower方法,它将无效。未使用@Query注释的方法由其名称解释。重命名它会导致像您这样的错误,它无法找到属性。 IsActive1不存在,例如

解决方案:重命名upper方法并确保实体中存在所有属性。特别是因为它说: No property dd found for type TbiDDCustomQueryConfig!

你可能想要findByIdAndIsActive或类似的东西。

答案 1 :(得分:0)

我不认为问题来自上层

findByDdConfigIdAndIsActive (Integer ddConfigId,String isActive);

我有相同的方法

findByParentGroupIdIsNullAndDeleted(boolean deleted); //parentGroupId , deleted

它工作正常。 spring data jpa认为'DdConfigId'为'dd'所以Config是问题所在,也许spring数据采用'config'之类的关键字表达式与'AND'和'OR'相同