Java 如何使用hibernate增加数据库中的计数?
数据库中的初始计数为0。每次收到请求时,我都需要更新计数 更新查询 这里的计数最初为0 假设int count=10; query.setParameter(“count”,count); 它将只更新一次 输出是11。下次相同的查询更新时,相同的输出11将出现。但我需要12是输出。Java 如何使用hibernate增加数据库中的计数?,java,mysql,spring,hibernate,spring-mvc,Java,Mysql,Spring,Hibernate,Spring Mvc,数据库中的初始计数为0。每次收到请求时,我都需要更新计数 更新查询 这里的计数最初为0 假设int count=10; query.setParameter(“count”,count); 它将只更新一次 输出是11。下次相同的查询更新时,相同的输出11将出现。但我需要12是输出。 String hql = "Update User f set f.count =:count + 1 where f.userId =:userId"; Query query
String hql = "Update User f set f.count =:count + 1 where f.userId =:userId";
Query query = session.createQuery(hql);
query.setParameter("userId", userId);
//query.setParameter("count", count);
result = query.executeUpdate();
计数未更新。看起来不错
谢谢你 你试过了吗
"Update User f set f.count=(f.count + 1) where f.userId =:userId";
你试过了吗
"Update User f set f.count=(f.count + 1) where f.userId =:userId";
此代码段将不会运行。“:count”参数从未设置为查询对象。@SudoRahul我添加了query.setParameter(“count”,count);但是它没有更新,你传递了旧的计数值吗?@SudoRahul是的。我不想要基于db count的SetParameter,它将每次递增1,然后“ruslanys”的答案应该是你应该签出的答案。此代码段将不会运行。“:count”参数从未设置为查询对象。@SudoRahul我添加了query.setParameter(“count”,count);但是它没有更新,你传递了旧的计数值吗?@SudoRahul是的。我不想要基于db count的setParameter它每次都会增加1,那么“ruslanys”的答案应该是你应该检查的答案。