【问题标题】:How can I fix bugs in Custom Repository?如何修复自定义存储库中的错误?
【发布时间】:2022-01-05 12:53:20
【问题描述】:

我正在尝试使用 spring-data-jpa 创建一个自定义存储库。

我的代码

存储库

@NoRepositoryBean
interface CustomRepository<T, ID : Serializable> : JpaRepository<T, ID> {
    fun findBySomeRestrict(id: ID): T?
}

RepositoryImpl

@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
    }

}

主类

@EnableJpaAuditing
@EnableJpaRepositories(repositoryBaseClass = CustomRepositoryImpl::class)
@SpringBootApplication
class ForSubmitApplication

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

我的错误消息

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
  • 反编译代码

【问题讨论】:

标签: spring-boot jpa spring-data-jpa


【解决方案1】:

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

你想follow the steps outlined in the documentation

  1. 创建一个只包含自定义方法的接口。你目前没有那个。

  2. 创建它的实现。您目前正在实现最终的 Repository 接口,这是错误的。

  3. 从步骤 1 中创建的自定义界面扩展您的最终存储库界面。

【讨论】:

  • 感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 2020-04-01
  • 1970-01-01
  • 2016-03-27
相关资源
最近更新 更多