Giter Club home page Giter Club logo

spring-data-jpa-projections-example's People

Contributors

sohangp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

spring-data-jpa-projections-example's Issues

params in sql and paging

hi, I have read your medium post about this project, and run the project, it's great to put native query in orm.xml and map two tables.

But for real-world cases, queries often have params and the result need paging, how to do it with JPA-projection, would you please make the example go deeper?

I have a complex sql as below, the beenUsed field is neither from a nor b table, how do I mapping it to a dto?

    select
        a.*,
        (case
            when count(b.rule_id) > 0 then 'Y'
            else 'N'
        end) beenUsed
    from
        t_rule a
    left join
        t_ruleset_rule_rel b
            on a.id = b.rule_id
    where
        if(? is not null ,a.name like concat('%',?,'%') ,1=1)
        and if(? > 0 ,a.type in (?, ?) ,1=1)
        and if(? is not null ,a.create_time >= ? ,1=1)
        and if(? is not null ,a.create_time <= ? ,1=1)
    group by
        a.id
    order by
        a.create_time desc limit ?

currently, I write sql like this, but get syntax error, would you please give some suggestion?

    @Query(value = "select new com.coolproject.dto.RuleRec(a.id as id,a.name as name, a.desc as desc, a.type as type, a.value as value, a.status as status, a.create_time as createTime, a.create_user as createUser, a.update_time as updateTime, a.update_user as updateUser, (case when count(b.rule_id) > 0 then 'Y' else 'N' end) as beenUsed) " +
            "from t_rule a left join t_ruleset_rule_rel b on a.id = b.rule_id where " +
            "if(:#{#param.name} is not null ,a.name like concat('%',:#{#param.name},'%') ,1=1) " +
            "and if(:#{#param.type.size()} > 0 ,a.type in :#{#param.type} ,1=1)" +
            "and if(:#{#param.startDate} is not null ,a.create_time >= :#{#param.startDate} ,1=1) " +
            "and if(:#{#param.endDate} is not null ,a.create_time <= :#{#param.endDate} ,1=1) " +
            "group by a.id", nativeQuery = true)
    Page<RuleRec> ruleRecList(@Param("param") RuleQueryParam param, Pageable pageable);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.