Generic criteriaUpdate set boolean = !boolean
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
spring-boot spring-data-jpa criteria hibernate-criteria criteria-api
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
add a comment |
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
add a comment |
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);
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
answered Nov 11 at 9:37
pirho
3,451101730
3,451101730
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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