How to create a temporary column + when + order by with Criteria Builder












0















here is the sql statement I am trying to translate in jpa :



  select 
id,
act_invalidation_id,
last_modification_date,
title,
case when act_invalidation_id is null then 1 else 0 end as test
from act order by test, last_modification_date desc


The actual translation



Root<Act> act = query.from(Act.class);

builder.selectCase()
.when(builder.isNull(actRoot.get("actInvalidation")), 1)
.otherwise(0).as(Integer.class);
Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
Order byInvalidationOrder = builder.asc(actInvalidationPath);

Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
Order byLastModificationDate = builder.desc(publicationDate);

query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
entityManager.createQuery(query).getResultList();


I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.



In short: How to create a temp column with integer values, then order by this temp column in jpa ?



Thank you










share|improve this question





























    0















    here is the sql statement I am trying to translate in jpa :



      select 
    id,
    act_invalidation_id,
    last_modification_date,
    title,
    case when act_invalidation_id is null then 1 else 0 end as test
    from act order by test, last_modification_date desc


    The actual translation



    Root<Act> act = query.from(Act.class);

    builder.selectCase()
    .when(builder.isNull(actRoot.get("actInvalidation")), 1)
    .otherwise(0).as(Integer.class);
    Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
    Order byInvalidationOrder = builder.asc(actInvalidationPath);

    Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
    Order byLastModificationDate = builder.desc(publicationDate);

    query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
    entityManager.createQuery(query).getResultList();


    I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.



    In short: How to create a temp column with integer values, then order by this temp column in jpa ?



    Thank you










    share|improve this question



























      0












      0








      0








      here is the sql statement I am trying to translate in jpa :



        select 
      id,
      act_invalidation_id,
      last_modification_date,
      title,
      case when act_invalidation_id is null then 1 else 0 end as test
      from act order by test, last_modification_date desc


      The actual translation



      Root<Act> act = query.from(Act.class);

      builder.selectCase()
      .when(builder.isNull(actRoot.get("actInvalidation")), 1)
      .otherwise(0).as(Integer.class);
      Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
      Order byInvalidationOrder = builder.asc(actInvalidationPath);

      Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
      Order byLastModificationDate = builder.desc(publicationDate);

      query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
      entityManager.createQuery(query).getResultList();


      I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.



      In short: How to create a temp column with integer values, then order by this temp column in jpa ?



      Thank you










      share|improve this question
















      here is the sql statement I am trying to translate in jpa :



        select 
      id,
      act_invalidation_id,
      last_modification_date,
      title,
      case when act_invalidation_id is null then 1 else 0 end as test
      from act order by test, last_modification_date desc


      The actual translation



      Root<Act> act = query.from(Act.class);

      builder.selectCase()
      .when(builder.isNull(actRoot.get("actInvalidation")), 1)
      .otherwise(0).as(Integer.class);
      Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
      Order byInvalidationOrder = builder.asc(actInvalidationPath);

      Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
      Order byLastModificationDate = builder.desc(publicationDate);

      query.select(act).orderBy(byInvalidationOrder, byLastModificationDate);
      entityManager.createQuery(query).getResultList();


      I try to create a temporary column (named test) of Integer type and orderby this column, then orderby lastmodificationdate. The content of this new column is determined by the value of actInvalidation field.



      In short: How to create a temp column with integer values, then order by this temp column in jpa ?



      Thank you







      sql-order-by hibernate-criteria temp-tables






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 20:35







      dotista2008

















      asked Dec 31 '18 at 9:23









      dotista2008dotista2008

      307




      307
























          1 Answer
          1






          active

          oldest

          votes


















          1














          I didn't test this but it should work like this:



          Root<Act> act = query.from(Act.class);

          Expression<?> test = builder.selectCase()
          .when(builder.isNull(actRoot.get("actInvalidation")), 1)
          .otherwise(0).as(Integer.class);
          Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
          Order byInvalidationOrder = builder.asc(actInvalidationPath);

          Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
          Order byLastModificationDate = builder.desc(publicationDate);

          Order byTest = builder.asc(test);

          query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
          entityManager.createQuery(query).getResultList();





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985713%2fhow-to-create-a-temporary-column-when-order-by-with-criteria-builder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I didn't test this but it should work like this:



            Root<Act> act = query.from(Act.class);

            Expression<?> test = builder.selectCase()
            .when(builder.isNull(actRoot.get("actInvalidation")), 1)
            .otherwise(0).as(Integer.class);
            Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
            Order byInvalidationOrder = builder.asc(actInvalidationPath);

            Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
            Order byLastModificationDate = builder.desc(publicationDate);

            Order byTest = builder.asc(test);

            query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
            entityManager.createQuery(query).getResultList();





            share|improve this answer




























              1














              I didn't test this but it should work like this:



              Root<Act> act = query.from(Act.class);

              Expression<?> test = builder.selectCase()
              .when(builder.isNull(actRoot.get("actInvalidation")), 1)
              .otherwise(0).as(Integer.class);
              Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
              Order byInvalidationOrder = builder.asc(actInvalidationPath);

              Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
              Order byLastModificationDate = builder.desc(publicationDate);

              Order byTest = builder.asc(test);

              query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
              entityManager.createQuery(query).getResultList();





              share|improve this answer


























                1












                1








                1







                I didn't test this but it should work like this:



                Root<Act> act = query.from(Act.class);

                Expression<?> test = builder.selectCase()
                .when(builder.isNull(actRoot.get("actInvalidation")), 1)
                .otherwise(0).as(Integer.class);
                Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
                Order byInvalidationOrder = builder.asc(actInvalidationPath);

                Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
                Order byLastModificationDate = builder.desc(publicationDate);

                Order byTest = builder.asc(test);

                query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
                entityManager.createQuery(query).getResultList();





                share|improve this answer













                I didn't test this but it should work like this:



                Root<Act> act = query.from(Act.class);

                Expression<?> test = builder.selectCase()
                .when(builder.isNull(actRoot.get("actInvalidation")), 1)
                .otherwise(0).as(Integer.class);
                Expression<?> actInvalidationPath = actRoot.get("actInvalidation");
                Order byInvalidationOrder = builder.asc(actInvalidationPath);

                Path<Date> publicationDate = actRoot.get("metadata").get("publicationDate");
                Order byLastModificationDate = builder.desc(publicationDate);

                Order byTest = builder.asc(test);

                query.select(act).orderBy(byTest, byInvalidationOrder, byLastModificationDate);
                entityManager.createQuery(query).getResultList();






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 31 '18 at 12:58









                Simon MartinelliSimon Martinelli

                6,15811230




                6,15811230






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53985713%2fhow-to-create-a-temporary-column-when-order-by-with-criteria-builder%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas