سؤال

I'm trying to switch boolean field using the following hql:

update Entity e set e.booleanField = not e.booleanField where e.id = ?1;

Unfortunately "QuerySyntaxException: unexpected token: not near..." was thrown.

My question is: is there some unary operator for hibernate that supports such expression? Or any well known trick?

sql supports such queries (postgresql):

update entity_table set booleanField = not(booleanField);
هل كانت مفيدة؟

المحلول

I would just use a native SQL query for this.

نصائح أخرى

To toggle the boolean property, equivalent to the not unary operator, under HQL/JPQL, use:

UPDATE entity e SET e.booleanField = (e.booleanField=false)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top