arc*_*rch 1 java hibernate jpa
我正在尝试在 JPA 存储库中添加 2 个相同的方法名称。我不确定这是否可能。
这就是我实际上在 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);
}
Run Code Online (Sandbox Code Playgroud)
编译错误告诉我重命名该方法。我改变了第二种方法如下:
TbiDDCustomQueryConfig findByDdConfigIdAndIsActive1(Integer ddConfigId,String isActive);
Run Code Online (Sandbox Code Playgroud)
但是当我运行代码时,出现错误:
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
Run Code Online (Sandbox Code Playgroud)
小智 7
当我们出于某种原因想要有两个同名的方法时,接受答案并没有解决问题。(例如 findById 但具有不同的注释。)
根据spring data jpa文档。(https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)
find(或其他引入关键字)和 By 之间的任何文本都被认为是描述性的,除非使用结果限制关键字之一,例如 Distinct 在要创建的查询上设置不同标志或 Top/First 来限制查询结果。
您可以find...By在第二个方法名称之间添加文本,以将其与第一个方法分开,同时保持相同的功能。
IEfindSomeTextByDdConfigIdAndIsActive
@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 findSomeTextByDdConfigIdAndIsActive(Integer ddConfigId,String isActive);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |