使用 SQLQuery.uniqueResult() 执行本机 SQL INSERT 和 UPDATE

发布于 2024-09-03 10:26:16 字数 301 浏览 4 评论 0原文

我在项目中使用 Hibernate,并且在某些情况下我想使用 org.hibernate.SQLQuery 类上的 uniqueResult() 方法来执行本机 SQL < strong>INSERT 和UPDATE 操作。

我确实尝试在同一个类上使用 executeUpdate() 方法。但我收到一条错误消息,指出它们仅用于 HQL 更新。

请告知这是否是确保数据在数据库中保存/更新的有效且可靠的方法。

I am using Hibernate in my project and there is a certain scenario where I want to use the uniqueResult() method on the org.hibernate.SQLQuery class to perform native SQL INSERT and UPDATE operations.

I did try using the executeUpdate() method on the same class. But I get an error saying that they are used for HQL updates only.

Please advice if this is effective and reliable way of ensuring data being saved/updated in the database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2024-09-10 10:26:16

session.createSQLQuery() 用于查询,而不是操作。如果您想要执行原始 SQL 插入,请使用 session.connection() 和直接 JDBC 代码。

不知道为什么你需要这个,但我还建议检查 16.3。用于创建、更新和删除的自定义 SQL

session.createSQLQuery() is for querying, not manipulation. If you want to do raw SQL insert, use session.connection() and straight JDBC code.

Not sure why you need this exactly but I'd also suggest to check 16.3. Custom SQL for create, update and delete.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文