gpt4 book ai didi

java - jpa 存储库有 2 个相同的方法

转载 作者:行者123 更新时间:2023-11-30 08:01:20 26 4
gpt4 key购买 nike

我正在尝试在 JPA 存储库中添加 2 个相同的方法名称。我不确定这是否可能。

这就是我在 JPA 接口(interface)中实际尝试做的事情:

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

最佳答案

当我们出于某种原因想要有两个具有相同名称的方法时,接受答案没有解决问题。 (例如,findById 但具有不同的注释。)

根据spring data jpa文档。 (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation)

Any text between find (or other introducing keywords) and By is considered to be descriptive unless using one of the result-limiting keywords such as a Distinct to set a distinct flag on the query to be created or Top/First to limit query results.

您可以在第二个方法名称中的 find...By 之间添加文本,以将其与第一个方法分开,同时保持相同的功能。

findSomeTextByDdConfigIdAndIsActive

@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);

关于java - jpa 存储库有 2 个相同的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31872402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com