【问题标题】:Custom Repository with access to findAll specification method可以访问 findAll 规范方法的自定义存储库
【发布时间】:2017-06-27 01:39:06
【问题描述】:

需要在存储库层进行自定义动态搜索。无法通过查询方法或通过查询注释进行查询。 这个动态搜索需要调用pleJpaRepository spring类实现的findAll(Specification, Paging)

public interface CardsRepositoryCustom {
    public Page<Cards> customSearch(CardSearch CardSearch, Pageable page);
}

public interface CardsRepository extends JpaRepository<Cards, Integer>, CardsRepositoryCustom {

}

@Repository
public class CardsRepositoryImpl extends SimpleJpaRepository implements CardsRepositoryCustom{

    public CardsRepositoryImpl(Class<Cards> domainClass, EntityManager em) {
        super(domainClass, em);
    }

    @PersistenceContext
    private EntityManager em;

     @Override
     public Page<Cards> customSearch(CardSearch CardSearch, Pageable page) {
        Specification<Cards> specification = (Root<Cards> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
            ..
        }
        return this.findAll(specification, page);
     }
}


@Service
public class CardsServiceImpl implements CardsService {

    @Autowired
    public CardsServiceImpl(CardsRepository CardsRepository) {
        this.CardsRepository = CardsRepository;
    }

    public CardsRepository CardsRepository;

    @Override
    public Page<Cards> customSearch(CardSearch CardSearch, Pageable page) {
        ...
        return CardsRepository.customSearch(CardSearch, page);
    }
    ...

}

当我运行应用程序时,我得到了

com.dwengo.cards.repository.CardsRepositoryImpl 中构造函数的参数0 需要

【问题讨论】:

  • CardsRepositoryImpl 构造函数的第一个参数没有用,因为 Class 只能通过 Cards.class 获得。所以调用super时直接使用Cards.class。
  • 无任何更改 com.dwengo.cards.repository.CardsRepositoryImpl 中构造函数的参数 0 需要一个找不到的“java.lang.Class”类型的 bean。
  • 所以你没有从定义中删除它......你需要在构造函数上设置自动装配。
  • 当我们扩展 SimpleJpaRepository 类时,我们可以选择两个构造函数:(JpaEntityInformation entityInformation, EntityManager entityManager) 或 (Class domainClass, EntityManager em) 和 @Autowired public CardsRepositoryImpl(类 domainClass, EntityManager em) { super(Cards.class, em); } 不起作用
  • 不指定类,只做 super(MyDomain.class, em);

标签: java spring


【解决方案1】:

Spring 检测到需要构造 bean CardsRepositoryImpl 但是构造函数中没有定义 @Autowired 或 @Inject。所以 spring 尝试使用默认(无参数)构造函数创建一个实例。它失败是因为需要两个参数,而第一个(索引 0)是 Class 类型。

将@Autowired 添加到构造函数后,spring 尝试解决依赖关系,但找不到 Class 类型的 bean。完全可以理解。而且Class参数在这里也没用。

改变你的定义:

public class CardsRepositoryImpl extends SimpleJpaRepository<Cards, Long> implements CardsRepository {
    private final EntityManager em;
    @Autowired
    public CardsRepositoryImpl(EntityManager em) {
        super(Cards.class, em);
    }

    @Override
    public Page<Cards> customSearch(CardSearch CardSearch, Pageable page) {
        Specification<Cards> specification = (Root<Cards> root, CriteriaQuery<?> cq, CriteriaBuilder cb) -> {
        ..
        }
        return this.findAll(specification, page);
    }
}

【讨论】:

  • 这项工作,已经尝试在构造函数上放置自动装配,但没有考虑删除第一个参数......就像你说的那样没用。
猜你喜欢
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
  • 2017-08-10
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
相关资源
最近更新 更多