简体   繁体   English

自定义存储库实施未被识别

[英]Custom Repository Implementation is not being recognized

I am currently working on a project where I have created the following custom Repository:我目前正在开发一个项目,在该项目中创建了以下自定义存储库:

public interface ServiceRepository<T extends ServiceEntity> extends JpaRepository<T, UUID>, ServiceRepositoryCustom {
}

public interface ServiceRepositoryCustom {
    List<ServiceEntity> findAllContainingName(String query);
}

@Repository("Repo")
public class ServiceRepositoryCustomImpl implements ServiceRepositoryCustom {


    private final EntityManager em;

    public ServiceRepositoryCustomImpl(EntityManager em) {
        System.out.println("I got constructed");
        this.em = em;
    }

    @Override
    public List<ServiceEntity> findAllContainingName(String name) {
        System.out.println("I got called with: " + name);
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<ServiceEntity> cq = cb.createQuery(ServiceEntity.class);

        Root<ServiceEntity> serviceEntity = cq.from(ServiceEntity.class);
        List<Predicate> predicates = new ArrayList<>();

        if(name != null)  {
            // predicates.add(cb.equal(serviceEntity.get("name"), name));
            predicates.add(cb.like(serviceEntity.get("name"), name + "%"));
        }
        cq.where(predicates.toArray(predicates.toArray(new Predicate[0])));

        return em.createQuery(cq).getResultList();
    }
}

The print statement "I got called with: " never gets called.打印语句“我被调用:”永远不会被调用。 So for whatever reason Spring Boot is not running the method through my custom implementation.因此,无论出于何种原因,Spring Boot 都没有通过我的自定义实现运行该方法。

Any suggestions?有什么建议么? Any help is much appreciated任何帮助深表感谢

Edit: Here is the code that injects and uses the Repository in question编辑:这是注入和使用相关存储库的代码

@Repository
public interface PineappleServiceRepository extends ServiceRepository<PineappleServiceEntity> {
}

@Component("Registry")
@DependsOn({"Context", "Repo"})
public class Registry {
    private final List<ServiceRepository<? extends ServiceEntity>> serviceRepositories = new ArrayList<>();

    public Registry(PineappleServiceRepository pineappleServiceRepository) {
        this.serviceRepositories.add(pineappleServiceRepository);
    }
}

Edit 2: The code prints "I got constructed"编辑 2:代码打印“我得到了构建”

Edit 3: Class where findAllContainingName is called编辑 3:调用findAllContainingName的类

@RestController
@RequestMapping("/test")
@DependsOn("Registry")
public class ServiceController {

    private final Registry registry;

    public ServiceController(@NotNull Registry registry) {
        this.registry = registry;
    }

    @GetMapping("")
    List<ServiceEntity> all(@RequestParam("q") String query) {
        return getAllServices(query);
    }

    private @NotNull List<ServiceEntity> getAllServices(String query) {
        List<ServiceEntity> response = new ArrayList<>();
        for(ServiceRepository<? extends ServiceEntity> repo: this.registry.getServiceRepositories()){
             response.addAll(repo.findAllContainingName(query));
        }
        return response;
    }
}

Edit 4:编辑4:

Here the entities:这里的实体:

@Entity
@Table(name = "services")
public abstract class ServiceEntity {

    protected @Id
    UUID id = UUID.randomUUID();
    protected String name;

    // Constructor + Getters and Setters
}

@Entity
public class PineappleServiceEntity extends ServiceEntity {
    // Additional Properties, matching Constructors, Getters and Setters
}

So I was able to reproduce your problem and fix it.所以我能够重现您的问题并解决它。 Issue with your code is that your PineappleServiceRepository is not extending ServiceRepositoryCustom directly.您的代码的问题是您的PineappleServiceRepository没有直接扩展ServiceRepositoryCustom It seems your repository needs to implement it directly if you are accessing custom repository methods from that repository.如果您从该存储库访问自定义存储库方法,您的存储库似乎需要直接实现它。 I got that idea from this post.我从这篇文章中得到了这个想法。

So to fix your issue, either remove PineappleServiceRepository (as you don't have any properties in PineappleEntity) and use ServiceRepository to call that custom method or make PineappleServiceRepository extend ServiceRepositoryCustom .因此,要解决您的问题,请删除PineappleServiceRepository (因为您在 PineappleEntity 中没有任何属性)并使用ServiceRepository调用该自定义方法或使PineappleServiceRepository扩展ServiceRepositoryCustom

I have pushed changes to GitHub with fix.我已通过修复将更改推送到GitHub You can take a look.你可以看看。 If you want to keep PineappleServiceRepository and access custom method using this repository, let me know, I can update code.如果您想保留PineappleServiceRepository并使用此存储库访问自定义方法,请告诉我,我可以更新代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM