如何使用setMaxResults更新hibernate查询? [英] How to use update hibernate query using setMaxResults?

查看:129
本文介绍了如何使用setMaxResults更新hibernate查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



I hope it is the appropriate section, I have a problem with this code

Transaction transaction = session.beginTransaction(); 
Query query = session.createQuery("update database set floop= :ctrl1" +" where ctrl= :ctrl2 ").setMaxResults(2); 
query.setMaxResults(2);
query.setParameter("ctrl1",3);
query.setParameter("ctrl2", 5);

我通过 setMaxResults(2)只做前两个更新,他做了所有记录的更新,因为我做错了什么?感谢您的帮助

I ask through setMaxResults(2) to do the update only on the first two and he makes the update of all records as I do what is wrong?? thanks for any help

我以为使用 session.createSQLQuery ,但我不知道该怎么办。 / p>

I thought to use session.createSQLQuery, but I do not know how to do.

推荐答案

这个答案是张贴延迟,但对于那些正在查看数据库中更新行数的HQL

This answer is posting delay but it can be helpful for others user who is looking update number of rows in DB with limit using HQL


不幸setMaxResults()不工作更新并删除hibernate
查询。它仅适用于选择条件。

Unfortunatly setMaxResults() do not work update and delete hibernate query. It works only select criteria.

根据HQL,没有具体的解决方案可用,并且您想要更新具有限制数的行,然后请按照以下步骤操作:

As per HQL there is no specific solution is available and you want to update rows with number of limit then follow below steps


  1. 编写HQL以选择条件或范围为 -
    setMaxResults的所有行。它会返回一个带有限制的List对象。
  2. 然后更新特定的属性(你想更新的属性)和
    再次通过session.update()存储这些行的对象。方法。
  1. Write a HQL to select all rows with condition or range with
    setMaxResults. It will return you a List object with limit.
  2. Then update the specific property (Property you want to update) and store Objects of these rows again by session.update() method.

我假设tablename具有map Database 类,并且有两个变量 ctrl floop 与getter和setter( 根据您的问题

I'm assuming tablename with map Database class and there are two variable ctrl and floop with getter and setter(as per your question)

            List<Database> list = new ArrayList<>();
            Transaction transaction = session.beginTransaction(); 

            //Fetching record with limit 2 using setMaxResults()
            int setlimit = 2;
            String hql_query = "from Database where ctrl = :ctrl2";
            Query select_query = session.createQuery(hql_query).setMaxResults(setlimit);
            select_query.setParameter("ctrl2", 5);
            list = select_query.list();

            //iterating list and setting new value to particuler column or property
            int result;
            if (list != null) {
                for (Database element : list) {
                    element.setFloop(ctrl1);
                    //Here is updating data in database
                    session.update(element);
                }
                result = list.size();
            } else {
                result = 0;
            }
            System.out.println("Rows affected: " + result);

            transaction.commit();

这篇关于如何使用setMaxResults更新hibernate查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆