【问题标题】:Error creating a base repository class创建基础存储库类时出错
【发布时间】:2017-04-15 01:14:25
【问题描述】:
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.QueryDslJpaRepository;
import org.springframework.data.querydsl.EntityPathResolver;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;

@NoRepositoryBean
public class RepositorySupportImpl<T> extends QueryDslJpaRepository<T, Integer> implements RepositorySupport<T> {

    private EntityManager entityManager;


    public RepositorySupportImpl(JpaEntityInformation<T, Integer> entityInformation, EntityManager entityManager, EntityManager entityManager1) {
    super(entityInformation, entityManager);
    this.entityManager = entityManager1;
    }

    public RepositorySupportImpl(JpaEntityInformation<T, Integer> entityInformation, EntityManager entityManager, EntityPathResolver resolver, EntityManager entityManager1) {
    super(entityInformation, entityManager, resolver);
    this.entityManager = entityManager1;
    }

    @Override
    public EntityManager getEntityManager() {
    return this.entityManager;
    }

@Transactional
@Override
public <S extends T> S save(final S entity) {
    this.getEntityManager().persist(entity);
    return entity;
}

}

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;

import javax.persistence.EntityManager;

public interface RepositorySupport<T> extends JpaRepository<T, Integer>, QueryDslPredicateExecutor<T> {

    EntityManager getEntityManager();

}

在我的配置类中,我有 repositoryBaseClass = RepositorySupportImpl.class

但我收到此错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“registerService”的bean时出错:通过字段“userRepository”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“userRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalStateException:在类 ca.lighthouse.repository.RepositorySupportImpl 上找不到合适的构造函数来匹配给定的参数:[class org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation, class com.sun.proxy .$Proxy52]。确保你实现了一个构造函数来接受这些

【问题讨论】:

  • 您是否尝试在它要求的 RepositorySupportImpl 中添加所需的构造函数?
  • 是的,我让 ide 生成构造函数,但它不会创建这个构造函数,而且我不确定“com.sun.proxy.$Proxy52”的预期。
  • 我认为根据该页面 javacodegeeks.com/2012/08/… 您可能缺少 Class> springDataRepositoryInterface。试着按照他们在那里的做法告诉我们。
  • 我在stackoverflow.com/questions/53638214/…遇到了同样的问题

标签: spring-mvc


【解决方案1】:

我刚才也遇到了同样的问题,经过一番调试,我找到了解决办法:

确保您只有一个 EnableJpaRepositories 注释并且它指向实现类(而不是接口):

@EnableJpaRepositories(repositoryBaseClass = GenericJpaRepositoryImpl.class)

我希望它对未来的人有所帮助;)

【讨论】:

  • 单独的问题(相同的错误消息),但这是我的问题,是的 - 它需要指向 IMPL 而不是接口。
【解决方案2】:

我已经遇到了同样的问题,解决方案是创建正确的构造函数来实现存储库。

    public RepositorySupportImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
    super(entityInformation, entityManager);
}

【讨论】:

    【解决方案3】:

    你必须在 Impl 类中使用这两个构造函数:

     public GenericRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
        super(entityInformation, entityManager);
        this.em = entityManager;
        domainClass = null;
    }
    
    
    public GenericRepositoryImpl(Class<T> domainClass, EntityManager entityManager) {
        super(domainClass, entityManager);
        this.em = entityManager;
        this.domainClass = domainClass;
    }
    

    【讨论】:

      【解决方案4】:

      感谢您的所有 cmets,它现在可以工作了。我希望我能明确指出问题所在,但我做不到。这是场景,我有一个 spring mvc 应用程序,带有控制器、实体、业务服务、jsps,我想清理一些东西,所以我决定将项目分解为模块。所以我创建了一个新项目,添加了模块,然后将旧项目中的文件复制到新项目中。当我复制 RepositorySupport 类和接口时,我想我应该将它重命名为您在上面看到的内容。这导致了这个错误,经过几天的研究和尝试不同的事情,我决定复制到原始文件中并且它有效。这就是我所做的一切,复制文件并更新参考。

      【讨论】:

        猜你喜欢
        • 2019-08-02
        • 1970-01-01
        • 2012-09-12
        • 1970-01-01
        • 1970-01-01
        • 2016-03-12
        • 1970-01-01
        • 2016-11-28
        相关资源
        最近更新 更多