Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am new in Hibernate.

When i try to inverse boolean

@Query(value = "update ToDo t set t.done= (abs(t.done -1)) where t.id=:id") public void toogleDone(@Param("id") long id);

i have this exception:

[2014-01-20 16:20:38] java.lang.ClassCastException:

java.lang.Integer cannot be cast to java.lang.Long at org.hibernate.type.descriptor.java.LongTypeDescriptor.unwrap(LongTypeDescriptor.java:36) at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57) ...

when i try this query:

`@Query(value = "update ToDo t set t.done= (NOT t.done) where t.id=:id")

public void toogleDone(@Param("id") long id);`

i have exception:

[2014-01-20 16:28:58] unexpected AST node: not [update org.teamdev.todo.model.domain.ToDo t set t.done= (NOT t.done) where t.id=:id]

Do you anybody have any idea to inverse boolean in HQL?

share|improve this question
    
Try: set t.done = (t.done==false) –  acdcjunior Jan 20 '14 at 14:52
    
I had tried it too, i had exception unexpected token = near false –  Aventes Jan 20 '14 at 14:59
    
How about one = alone? set t.done = (t.done=false)? Or set t.done = (t.done < 1) or set t.done = ((t.done+0) < 1)? –  acdcjunior Jan 20 '14 at 19:36
    
Thank You very much! It`s work! –  Aventes Jan 20 '14 at 21:16

2 Answers 2

up vote 0 down vote accepted

Use set t.done = (t.done=false), so the Query would be:

update ToDo t set t.done = (t.done=false) where t.id=:id
share|improve this answer

You just want to toggle the bit value? You can accomplish this using CASE.

update ToDo t set t.done = (case when t.done = 1 then 0 else 1 end) where t.id=:id
share|improve this answer
    
Yes i tried. And i had in JPA console good result. But when it running in my program, it crashed with exception : Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryParameterException: Position beyond number of declared ordinal parameters. Remember that ordinal parameters are 1-based! Position: 1 at org.hibernate.ejb.QueryImpl.setParameter(QueryImpl.java:451) ... Im dont know, what the matter, but neither one from variants except for set t.done = (t.done=false) not working. –  Aventes Jan 20 '14 at 22:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.