如何在Hibernate中创建临时表?

发布于 2024-09-10 13:31:15 字数 1488 浏览 4 评论 0原文

目标

  1. 在 Hibernate 中调用 CREATE TEMPORARY TABLE 语句,而不使用本机 SQL。这意味着仅使用 HQL 或 Hibernate API。
  2. 将对象保存到临时表。
  3. 调用使用现有表和临时表的存储过程。
  4. 完成后DROP临时表。 (我知道没有必要,但我认为这样做是一个好习惯。)

背景

  1. 我对 SQL 非常熟悉,但对 Hibernate 还很陌生。
  2. 由于某人的决定,我被迫在项目中使用 Hibernate。
  3. 我要将 Web 表单保存到 Oracle 数据库。
  4. Web 表单包含一张充满文本字段(由其他人设计)的表格,每个单元格中都有一个。
  5. 当用户单击保存时,值必须保存在单个事务中。
  6. Web 表单由数据库视图支持。
  7. 数据库视图是使用 EAV 模式从数据库表创建的。 (这样做是因为列在某种程度上是动态的。)
  8. Web 表单中的每个文本字段均由数据库表中的一行建模。
  9. 显示 Web 表单在视图上使用 SELECT 语句。
  10. 更新 Web 表单在视图上使用 UPDATE 语句,该语句调用视图的 INSTEAD OF 触发器。
  11. 仅更新更改的值。每次更新都有审核跟踪。
  12. 如果其他用户在未通知用户的情况下更新了任何值,则事务将回滚。以下是此类场景的示例:(I) 当用户显示 Web 表单 (II) another 时,a 的值为 4用户将同一字段更新为 5 (III) 第一个用户将该字段更新为 2 并提交 Web 表单。

最初提出的解决方案

  1. 使用 AJAX (jQuery) 检测文本字段中的更改,并仅提交用户更改的内容。
  2. 但是,需要在数据库中检测到其他用户所做的更改。

解决方案应该工作得更好

  1. 当用户点击保存时,创建一个临时表(临时表是仅当前会话可见的表/连接,并在会话关闭/断开连接时自动删除)并将对象(单元格)保存到临时表中。
  2. 开始交易。
  3. 锁定一些现有表(或仅锁定相关行,以提高性能)。
  4. 将提交的数据与现有数据进行比较。
  5. 如果发生任何未被注意到的更改,请回滚事务。
  6. 更新必要的行。
  7. 提交事务并解锁表。
  8. 删除临时表。

有什么想法吗?

Goal

  1. Invoke a CREATE TEMPORARY TABLE statement in Hibernate without using native SQL. That means using HQL or Hibernate APIs only.
  2. Save objects to the temporary table.
  3. Invoke a stored procedure which makes use of existing tables and the temporary table.
  4. DROP the temporary table when finished. (I know it's not necessary, but I think it's a good habit to do so.)

Background

  1. I'm very familiar with SQL but new to Hibernate.
  2. I'm forced to use Hibernate in a project because of, you know, someone's decision.
  3. I'm going to save a web form to an Oracle database.
  4. The web form contains a table full of text fields (designed by someone else), one in each cell.
  5. When the user clicks Save, the values MUST be saved in a single transaction.
  6. The web form is backed up by a database view.
  7. The database view is created from a database table using the EAV pattern.
    (It is done so because the columns are somehow dynamic.)
  8. Each text field in the web form is modeled by one row in the database table.
  9. Displaying the web form uses SELECT statements on the view.
  10. Updating the web form uses UPDATE statements on the view, which calls the INSTEAD OF trigger of the view.
  11. Only changed values are updated. There is an audit trail for each update.
  12. If any of the values are updated by another user without the user's notice, the transaction is rolled back. Here is an example for such a scenario: (I) the value of a is 4 when the user displays the web form (II) another user updates the same field to 5 (III) the first user updates the field to 2 and submits the web form.

Originally Proposed Solution

  1. Use AJAX (jQuery) to detect changes in the text fields, and submit only those changed by the user.
  2. However, changes made by another user need to be detected in the database.

Solution Supposed to Work Better

  1. When the user clicks Save, create a temporary table (a temporary table is a table only seen by the current session / connection, and is dropped automatically when the session is closed / upon disconnection) and save the objects (cells) into the temporary table.
  2. Start a transaction.
  3. Lock some of the existing tables (or only related rows, for performance).
  4. Compare the submitted data with the existing data.
  5. If any unnoticed change was made, rollback the transaction.
  6. Update the necessary rows.
  7. Commit the transaction and unlock the tables.
  8. Drop the temporary table.

Is there any ideas?

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

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

发布评论

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

评论(2

温馨耳语 2024-09-17 13:31:15

这并不能满足您的确切要求,但考虑到没有尝试给出答案...您是否考虑过使用临时 CSV 表,例如 http://csvjdbc.sourceforge.net/

虽然它不符合通过 hibernate 执行此操作的要求,但它与数据库无关且跨平台。

This does not answer your exact requirements but given that there have been no attempts at an answer... have you considered using a temporary CSV table using something like http://csvjdbc.sourceforge.net/.

While it does not conform to requirement of doing it through hibernate, it is database agnostic and cross platform.

心意如水 2024-09-17 13:31:15

我认为这是使用乐观锁概念的经典场景。它是在实体中称为版本的新字段的帮助下实现的,该字段是递增的数字或时间戳。 @version 注释(在休眠中)将确保您拥有增量版本号。

这是这个概念的演练。

  1. (I) 当用户显示 Web 表单时,a 的值为 4 - fetch
    记录的版本以及其他字段。说一下版本

  2. (II) 另一个用户将同一字段更新为 5 - 另一个用户会
    将版本更改为其他数字。假设是版本 2。

  3. (III) 第一个用户将字段更新为 2 并提交网络表单。

    • 保存记录时,更新语句将如下所示 -
    update user_table
    set somefield = value
    where version = 1  --(as web form contains version 1 of the record)

这样您就不会替换其他人的记录。 Hibernate 确实可以为您提供更多帮助。它捕获这种情况并抛出 OptimisticLockException,您可以使用它来做出任何异常决定。

I see it is a classic scenario to use the optimistic lock concept. It is achieved with the help of a new field in your entity called version which is an increasing number or timestamp. @version annotation (in hibernate) will ensure you have an incremental version number.

Here is a dry run of the concept.

  1. (I) the value of a is 4 when the user displays the web form - fetch
    the version of the record along with other fields. Let's say the version

  2. (II) another user updates the same field to 5 - Another user would
    change the version to some other number. Let's say Version 2.

  3. (III) the first user updates the field to 2 and submits the webform.

    • while saving the record the update statement will look like this -
    update user_table
    set somefield = value
    where version = 1  --(as web form contains version 1 of the record)

This way you are not replacing someone else's record. Hibernate does help you with a little bit more here. It catches this scenario and throws an OptimisticLockException and you can use it to take any exceptional decision.

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