如何修复Hibernate中Update查询的已弃用的RISKQuery方法?

pwuypxnk  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(134)

我正在将遗留代码升级到Hibernate 6。我收到了用于更新查询的RESTQuery方法的弃用警告。如何修复此代码?Google搜索只给出了旧API的示例。

public static int setLastIpTimeStamp(Timestamp ipLastTimeStamp)
        {
            int rowsaffected = -1;
            try (Session session = SessionFactoryManager.getSessionFactory().openSession())
            {
                Transaction tx = session.beginTransaction();
                try
                {
                    Query query = session.createQuery("UPDATE HistoryDataProcessorSettingsEntity row SET row.ipLastTimeStamp=:ipLastTimeStamp WHERE row.id=:id ")
                            .setParameter("id", 1)
                            .setParameter("ipLastTimeStamp", ipLastTimeStamp.toInstant());
                    rowsaffected = query.executeUpdate();
                    tx.commit();
                }
                catch (Exception e)
                {
                    tx.rollback();
                    if (Logger.isErrorEnabled())
                        Logger.error(e);
                }
            }
            catch (Exception e)
            {
                if (Logger.isErrorEnabled())
                    Logger.error(e);
            }
            return rowsaffected;
        }
uyhoqukh

uyhoqukh1#

根据最新的javadoc,你应该使用createMutationQuery(),因为你只做更新:

MutationQuery query = session.createMutationQuery("UPDATE HistoryDataProcessorSettingsEntity row SET row.ipLastTimeStamp=:ipLastTimeStamp WHERE row.id=:id ")
                            .setParameter("id", 1)
                            .setParameter("ipLastTimeStamp", ipLastTimeStamp.toInstant());

相关问题