Generic criteriaUpdate set boolean = !boolean











up vote
2
down vote

favorite
1












I want to use criteriaUpdate to create an update query like this:



UPDATE <SOME TABLE>
SET SELECTED = !SELECTED
WHERE
[DYNAMIC QUERY HERE]


The closest I could get was with the code:



public <T> Query createRevertSelectionQuery(Class<T> clazz, EntityManager em, Specification<T> s) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<T> criteriaUpdate = cb.createCriteriaUpdate(clazz);
Root<T> root = criteriaUpdate.from(clazz);
Predicate p = cb.and(new Predicate {s.toPredicate(root, null, cb)});
Expression<Boolean> e =cb.not((root.get("selected").as(Boolean.class)));
Path<Boolean> selected = root.get("selected");
criteriaUpdate.set(selected, e);
criteriaUpdate.where(p);
Query q = em.createQuery(criteriaUpdate);
return q;
}


but it fails because I get the following query:



update com.redknee.suspense.mgt.model.Moc as generatedAlias0 
set generatedAlias0.selected = generatedAlias0.selected <> true
where
[dynamic query]


giving me the error



org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: <> near line 1, column 118



Anyone can help please?










share|improve this question
























  • Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
    – pirho
    Nov 10 at 17:17










  • Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
    – Luis Marcilio Braga Oliveira
    Nov 10 at 18:05












  • No prob. And the Specification seems to be only for appending if there are other predicates.
    – pirho
    Nov 10 at 18:07






  • 1




    Wrong dialect maybe?
    – Antoniossss
    Nov 10 at 18:08















up vote
2
down vote

favorite
1












I want to use criteriaUpdate to create an update query like this:



UPDATE <SOME TABLE>
SET SELECTED = !SELECTED
WHERE
[DYNAMIC QUERY HERE]


The closest I could get was with the code:



public <T> Query createRevertSelectionQuery(Class<T> clazz, EntityManager em, Specification<T> s) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<T> criteriaUpdate = cb.createCriteriaUpdate(clazz);
Root<T> root = criteriaUpdate.from(clazz);
Predicate p = cb.and(new Predicate {s.toPredicate(root, null, cb)});
Expression<Boolean> e =cb.not((root.get("selected").as(Boolean.class)));
Path<Boolean> selected = root.get("selected");
criteriaUpdate.set(selected, e);
criteriaUpdate.where(p);
Query q = em.createQuery(criteriaUpdate);
return q;
}


but it fails because I get the following query:



update com.redknee.suspense.mgt.model.Moc as generatedAlias0 
set generatedAlias0.selected = generatedAlias0.selected <> true
where
[dynamic query]


giving me the error



org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: <> near line 1, column 118



Anyone can help please?










share|improve this question
























  • Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
    – pirho
    Nov 10 at 17:17










  • Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
    – Luis Marcilio Braga Oliveira
    Nov 10 at 18:05












  • No prob. And the Specification seems to be only for appending if there are other predicates.
    – pirho
    Nov 10 at 18:07






  • 1




    Wrong dialect maybe?
    – Antoniossss
    Nov 10 at 18:08













up vote
2
down vote

favorite
1









up vote
2
down vote

favorite
1






1





I want to use criteriaUpdate to create an update query like this:



UPDATE <SOME TABLE>
SET SELECTED = !SELECTED
WHERE
[DYNAMIC QUERY HERE]


The closest I could get was with the code:



public <T> Query createRevertSelectionQuery(Class<T> clazz, EntityManager em, Specification<T> s) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<T> criteriaUpdate = cb.createCriteriaUpdate(clazz);
Root<T> root = criteriaUpdate.from(clazz);
Predicate p = cb.and(new Predicate {s.toPredicate(root, null, cb)});
Expression<Boolean> e =cb.not((root.get("selected").as(Boolean.class)));
Path<Boolean> selected = root.get("selected");
criteriaUpdate.set(selected, e);
criteriaUpdate.where(p);
Query q = em.createQuery(criteriaUpdate);
return q;
}


but it fails because I get the following query:



update com.redknee.suspense.mgt.model.Moc as generatedAlias0 
set generatedAlias0.selected = generatedAlias0.selected <> true
where
[dynamic query]


giving me the error



org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: <> near line 1, column 118



Anyone can help please?










share|improve this question















I want to use criteriaUpdate to create an update query like this:



UPDATE <SOME TABLE>
SET SELECTED = !SELECTED
WHERE
[DYNAMIC QUERY HERE]


The closest I could get was with the code:



public <T> Query createRevertSelectionQuery(Class<T> clazz, EntityManager em, Specification<T> s) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<T> criteriaUpdate = cb.createCriteriaUpdate(clazz);
Root<T> root = criteriaUpdate.from(clazz);
Predicate p = cb.and(new Predicate {s.toPredicate(root, null, cb)});
Expression<Boolean> e =cb.not((root.get("selected").as(Boolean.class)));
Path<Boolean> selected = root.get("selected");
criteriaUpdate.set(selected, e);
criteriaUpdate.where(p);
Query q = em.createQuery(criteriaUpdate);
return q;
}


but it fails because I get the following query:



update com.redknee.suspense.mgt.model.Moc as generatedAlias0 
set generatedAlias0.selected = generatedAlias0.selected <> true
where
[dynamic query]


giving me the error



org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: <> near line 1, column 118



Anyone can help please?







spring-boot spring-data-jpa criteria hibernate-criteria criteria-api






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 18:04

























asked Nov 10 at 16:36









Luis Marcilio Braga Oliveira

132




132












  • Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
    – pirho
    Nov 10 at 17:17










  • Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
    – Luis Marcilio Braga Oliveira
    Nov 10 at 18:05












  • No prob. And the Specification seems to be only for appending if there are other predicates.
    – pirho
    Nov 10 at 18:07






  • 1




    Wrong dialect maybe?
    – Antoniossss
    Nov 10 at 18:08


















  • Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
    – pirho
    Nov 10 at 17:17










  • Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
    – Luis Marcilio Braga Oliveira
    Nov 10 at 18:05












  • No prob. And the Specification seems to be only for appending if there are other predicates.
    – pirho
    Nov 10 at 18:07






  • 1




    Wrong dialect maybe?
    – Antoniossss
    Nov 10 at 18:08
















Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
– pirho
Nov 10 at 17:17




Seems like spring, spring-boot? Add relevant tag to question if is. How about the specification?
– pirho
Nov 10 at 17:17












Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
– Luis Marcilio Braga Oliveira
Nov 10 at 18:05






Thank you @pirho. The specification is the following: I want to update a generic entity T with the where clause specified in "Specification s" set the column "selected" = "NOT(selected)" If I could make "set generatedAlias0.selected = (generatedAlias0.selected <> true) " maybe it would work, but I don't know how to do it...
– Luis Marcilio Braga Oliveira
Nov 10 at 18:05














No prob. And the Specification seems to be only for appending if there are other predicates.
– pirho
Nov 10 at 18:07




No prob. And the Specification seems to be only for appending if there are other predicates.
– pirho
Nov 10 at 18:07




1




1




Wrong dialect maybe?
– Antoniossss
Nov 10 at 18:08




Wrong dialect maybe?
– Antoniossss
Nov 10 at 18:08












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I am not sure if this is a bug or if it is just not meant to be used this way .



In where-clause NOT and any other operands work like a charm. But, no matter what you try Hibernate query builder seems always to optimize those parenthesis away (in my opinion it might still be a good habit to always use parenthesis but its only an opinion).



One way to force parenthesis is to use JPA Subquery. See below example. Note that i have slightly altered the JPA object names by my own taste and not included the Specification because it is not relevant to this solution:



CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaUpdate<T> update = cb.createCriteriaUpdate(clazz);
Root<T> from = update.from(clazz);
Path<Boolean> selected = from.get("selected");

// Subquery just "joins back" to the same row and
// returns a negated boolean value of "selected" from the original row
Subquery<Boolean> subSelect = update.subquery(Boolean.class);
Root<T> subFrom = subSelect.from(clazz);
subSelect.select(cb.not(selected));
subSelect.where(cb.equal(from.get("id"), subFrom.get("id")));

update.set(selected, subSelect);





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',
    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%2f53241053%2fgeneric-criteriaupdate-set-boolean-boolean%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








    up vote
    0
    down vote



    accepted










    I am not sure if this is a bug or if it is just not meant to be used this way .



    In where-clause NOT and any other operands work like a charm. But, no matter what you try Hibernate query builder seems always to optimize those parenthesis away (in my opinion it might still be a good habit to always use parenthesis but its only an opinion).



    One way to force parenthesis is to use JPA Subquery. See below example. Note that i have slightly altered the JPA object names by my own taste and not included the Specification because it is not relevant to this solution:



    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaUpdate<T> update = cb.createCriteriaUpdate(clazz);
    Root<T> from = update.from(clazz);
    Path<Boolean> selected = from.get("selected");

    // Subquery just "joins back" to the same row and
    // returns a negated boolean value of "selected" from the original row
    Subquery<Boolean> subSelect = update.subquery(Boolean.class);
    Root<T> subFrom = subSelect.from(clazz);
    subSelect.select(cb.not(selected));
    subSelect.where(cb.equal(from.get("id"), subFrom.get("id")));

    update.set(selected, subSelect);





    share|improve this answer

























      up vote
      0
      down vote



      accepted










      I am not sure if this is a bug or if it is just not meant to be used this way .



      In where-clause NOT and any other operands work like a charm. But, no matter what you try Hibernate query builder seems always to optimize those parenthesis away (in my opinion it might still be a good habit to always use parenthesis but its only an opinion).



      One way to force parenthesis is to use JPA Subquery. See below example. Note that i have slightly altered the JPA object names by my own taste and not included the Specification because it is not relevant to this solution:



      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaUpdate<T> update = cb.createCriteriaUpdate(clazz);
      Root<T> from = update.from(clazz);
      Path<Boolean> selected = from.get("selected");

      // Subquery just "joins back" to the same row and
      // returns a negated boolean value of "selected" from the original row
      Subquery<Boolean> subSelect = update.subquery(Boolean.class);
      Root<T> subFrom = subSelect.from(clazz);
      subSelect.select(cb.not(selected));
      subSelect.where(cb.equal(from.get("id"), subFrom.get("id")));

      update.set(selected, subSelect);





      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I am not sure if this is a bug or if it is just not meant to be used this way .



        In where-clause NOT and any other operands work like a charm. But, no matter what you try Hibernate query builder seems always to optimize those parenthesis away (in my opinion it might still be a good habit to always use parenthesis but its only an opinion).



        One way to force parenthesis is to use JPA Subquery. See below example. Note that i have slightly altered the JPA object names by my own taste and not included the Specification because it is not relevant to this solution:



        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaUpdate<T> update = cb.createCriteriaUpdate(clazz);
        Root<T> from = update.from(clazz);
        Path<Boolean> selected = from.get("selected");

        // Subquery just "joins back" to the same row and
        // returns a negated boolean value of "selected" from the original row
        Subquery<Boolean> subSelect = update.subquery(Boolean.class);
        Root<T> subFrom = subSelect.from(clazz);
        subSelect.select(cb.not(selected));
        subSelect.where(cb.equal(from.get("id"), subFrom.get("id")));

        update.set(selected, subSelect);





        share|improve this answer












        I am not sure if this is a bug or if it is just not meant to be used this way .



        In where-clause NOT and any other operands work like a charm. But, no matter what you try Hibernate query builder seems always to optimize those parenthesis away (in my opinion it might still be a good habit to always use parenthesis but its only an opinion).



        One way to force parenthesis is to use JPA Subquery. See below example. Note that i have slightly altered the JPA object names by my own taste and not included the Specification because it is not relevant to this solution:



        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaUpdate<T> update = cb.createCriteriaUpdate(clazz);
        Root<T> from = update.from(clazz);
        Path<Boolean> selected = from.get("selected");

        // Subquery just "joins back" to the same row and
        // returns a negated boolean value of "selected" from the original row
        Subquery<Boolean> subSelect = update.subquery(Boolean.class);
        Root<T> subFrom = subSelect.from(clazz);
        subSelect.select(cb.not(selected));
        subSelect.where(cb.equal(from.get("id"), subFrom.get("id")));

        update.set(selected, subSelect);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 9:37









        pirho

        3,451101730




        3,451101730






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241053%2fgeneric-criteriaupdate-set-boolean-boolean%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

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation