首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复自定义存储库中的bug?

如何修复自定义存储库中的bug?
EN

Stack Overflow用户
提问于 2021-11-28 08:24:39
回答 1查看 222关注 0票数 0

我试图用spring建立一个自定义存储库。

我的密码

存储库

代码语言:javascript
复制
@NoRepositoryBean
interface CustomRepository<T, ID : Serializable> : JpaRepository<T, ID> {
    fun findBySomeRestrict(id: ID): T?
}

RepositoryImpl

代码语言:javascript
复制
@Transactional(readOnly = true)
class CustomRepositoryImpl<T, ID : Serializable>(
    private val entityManager: EntityManager,
    jpaEntityInformation: JpaMetamodelEntityInformation<T, ID>
) : SimpleJpaRepository<T, ID>(jpaEntityInformation, entityManager), CustomRepository<T, ID> {

    override fun findBySomeRestrict(id: ID): T? {
        // TODO
    }

}

主班

代码语言:javascript
复制
@EnableJpaAuditing
@EnableJpaRepositories(repositoryBaseClass = CustomRepositoryImpl::class)
@SpringBootApplication
class ForSubmitApplication

fun main(args: Array<String>) {
    runApplication<ForSubmitApplication>(*args)
}

MyErrorMessage

代码语言:javascript
复制
Caused by: java.lang.IllegalStateException: No suitable constructor found on interface com.example.forsubmit.global.custom.CustomRepository to match the given arguments: org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation, jdk.proxy2.$Proxy132. Make sure you implement a constructor taking these
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.lambda$instantiateClass$6(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at java.base/java.util.Optional.orElseThrow(Optional.java:403) ~[na:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.instantiateClass(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getTargetRepositoryViaReflection(RepositoryFactorySupport.java:542) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:182) ~[spring-data-jpa-2.6.0.jar:2.6.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:164) ~[spring-data-jpa-2.6.0.jar:2.6.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:75) ~[spring-data-jpa-2.6.0.jar:2.6.0]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:324) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.lambda$afterPropertiesSet$5(RepositoryFactoryBeanSupport.java:322) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.util.Lazy.getNullable(Lazy.java:230) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.util.Lazy.get(Lazy.java:114) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:328) ~[spring-data-commons-2.6.0.jar:2.6.0]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:144) ~[spring-data-jpa-2.6.0.jar:2.6.0]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.13.jar:5.3.13]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.13.jar:5.3.13]
    ... 72 common frames omitted

我做了什么来修复它

  • 将构造函数类型更改为JpaEntityInformation
  • 反编译代码
EN

回答 1

Stack Overflow用户

发布于 2021-11-29 06:50:36

似乎您只想提供一个方法的实现。在这种情况下,您不能设置repositoryBaseClass

相反,您想要遵循文档中概述的步骤

  1. 创建一个只包含自定义方法的接口。你现在还没有那个。
  2. 创建一个它的实现。您目前正在实现最终存储库接口,这是错误的。
  3. 从步骤1中创建的自定义接口扩展最终的存储库接口。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70141724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档