简体   繁体   English

如何在 Spring Boot 中编写存储库查询以通过关联键进行过滤?

[英]How do you write a repository query in Spring boot to filter by an association key?

How do you write a query in a repository interface to do a WHERE assoc_id = :key?您如何在存储库界面中编写查询以执行 WHERE assoc_id = :key?

I have an entity called CmsImpacts , and then I have a service that calls the ImpactsRepository which looks like this:我有一个名为CmsImpacts的实体,然后我有一个调用ImpactsRepository的服务,如下所示:

    import java.util.List;
    
    public interface ImpactsRepository<T extends CmsImpacts> extends BaseCmsRepository<CmsImpacts> {
        
        @Query("SELECT a FROM #{#entityName} a where a.scenarioId = :scenarioId")
        List<CmsImpacts> getByScenarioPaged(
                Integer scenarioId,
                Pageable pageable
        );
        
        @Query("SELECT COUNT(a.id) FROM #{#entityName} a where a.scenarioId = :scenarioId")
        Integer getAsScenarioCount(
                Integer scenarioId
        );
        
        
        
    }

The gist of this relationship is that CmsScenarios is a one to many on CmsImpacts .这种关系的要点是CmsScenariosCmsImpacts上的CmsImpacts The entity relation is defined as:实体关系定义为:

    
    @JsonIgnore
    @JsonBackReference
    @JoinColumn(name = "scenario_id", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private CmsScenarios scenarioId;

Whenever I call my service and pass in the Integer value of the scenario_id, it throws a TypeException because (rightfully so), expects a CmsScenario object, not an Integer.每当我调用我的服务并传入 scenario_id 的 Integer 值时,它都会抛出一个 TypeException,因为(理所当然),它需要一个 CmsScenario 对象,而不是一个 Integer。

Error Message : 2020-11-04 15:10:45.764 ERROR 128396 --- [io-8080-exec-18] osbwservlet.support.ErrorPageFilter : Forwarding to error page from request [/api/impacts/list/scenario/2] due to exception [org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [2] did not match expected type [com.vw.asa.entities.cms.CmsScenarios (n/a)];错误消息:2020-11-04 15:10:45.764 ERROR 128396 --- [io-8080-exec-18] osbwservlet.support.ErrorPageFilter:从请求转发到错误页面 [/api/impacts/list/scenario/2 ] 由于异常 [org.springframework.dao.InvalidDataAccessApiUsageException: 参数值 [2] 与预期类型 [com.vw.asa.entities.cms.CmsScenarios (n/a)] 不匹配; nested exception is java.lang.IllegalArgumentException: Parameter value [2] did not match expected type [com.vw.asa.entities.cms.CmsScenarios (n/a)]]嵌套异常是 java.lang.IllegalArgumentException:参数值 [2] 与预期类型不匹配 [com.vw.asa.entities.cms.CmsScenarios (n/a)]]

What do I need to do in order to execute this query?我需要做什么才能执行此查询? This applies to the query above it also, the Paginated counter.这也适用于它上面的查询,即分页计数器。

Note:笔记:

I do not want to just retrieve the CmsScenarios object and call getImpacts() , that would return all objects, and I need to be able to paginate over them.我不想只检索CmsScenarios对象并调用getImpacts() ,这将返回所有对象,并且我需要能够对它们进行分页。

您必须添加相关实体的 id

@Query("SELECT a FROM #{#entityName} a where a.scenarioId.id = :scenarioId")

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

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