【问题标题】:How to increment count in database using hibernate?如何使用休眠增加数据库中的计数?
【发布时间】:2017-06-15 07:21:03
【问题描述】:

数据库中的初始计数为 0。每当有请求时,我都需要更新计数。

更新查询

这里的 count 最初是 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 = session.createQuery(hql);
        query.setParameter("userId", userId);
        //query.setParameter("count", count);
        result = query.executeUpdate();

计数未更新。看起来积极的重播。

谢谢你..!

【问题讨论】:

  • 这个 sn-p 不会运行。 ":count" 参数永远不会设置为查询对象。
  • @SudoRahul 我添加了 query.setParameter("count",count);但它没有更新
  • 你是否通过了旧的计数值?
  • @SudoRahul 是的。我不希望基于 db 计数的 setParamter 每次都会增加 1
  • 那么'ruslanys'的答案应该是你应该检查的答案。

标签: java mysql spring hibernate spring-mvc


【解决方案1】:

你试过了吗?

"Update User f set f.count=(f.count + 1) where f.userId =:userId";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2011-08-16
    • 1970-01-01
    相关资源
    最近更新 更多