简体   繁体   English

在 session 中保留 Hibernate/Grails 域 object

[英]Keep Hibernate/Grails domain object in session

I have a application where the user needs to enter data but the form is spread across several screens each requiring the user to POST data to the server as the user change pages.我有一个应用程序,用户需要输入数据,但表单分布在多个屏幕上,每个屏幕都要求用户在用户更改页面时将数据发布到服务器。

Normally, I would store a domain object in the session and have the user make changes to this object in the session.通常,我会在 session 中存储域 object 并让用户在 Z21D6F4440CFB5119A982E 中对此 object 进行更改Once all user changes are complete I would then save all changes at once to the DB.完成所有用户更改后,我将立即将所有更改保存到数据库中。 This form is not a flow, the user can go to one page and back freely.此表格不是一个流程,用户可以自由地翻页和翻页。

Using Grails, I want to know the best approach.使用 Grails,我想知道最好的方法。 This domain object is highly nested so creating a psudo-domain object for web layer (and then mapping field by field to the domain obj) is not an option.这个域 object 是高度嵌套的,因此为 web 层创建一个伪域 object(然后逐个字段映射到域 obj)不是一个选项。

I want to know if storing the Hibernate domain object in the session is a good idea?我想知道将 Hibernate 域 object 存储在 session 中是否是个好主意? I am having Hibernate session issues however due to the domain object being detached from the hibernate session. I am having Hibernate session issues however due to the domain object being detached from the hibernate session.

Very important: I cannot make saves to the database on each page change either.非常重要:我也无法在每次页面更改时保存到数据库。 I can only save the domain object to the DB once the user has completed all their changes.用户完成所有更改后,我只能将域 object 保存到数据库中。 As stated before, this could span several http requests.如前所述,这可能跨越多个 http 请求。 Each time, the user changes need to be temp stored somewhere.每次,用户更改都需要临时存储在某个地方。

I've tried several attempts to keep the domain obj in the session, then on each submit, reattach that domain object and make my updates, but grails flushes the session on each request so this causes incomplete domain objects to be saved. I've tried several attempts to keep the domain obj in the session, then on each submit, reattach that domain object and make my updates, but grails flushes the session on each request so this causes incomplete domain objects to be saved.

I am hoping someone could lend me some advice on storing user changes in memory, then all at once, commiting changes to the DB.我希望有人能给我一些关于在 memory 中存储用户更改的建议,然后一次性提交对数据库的更改。

Thanks谢谢

You should be able to:你应该能够:

  1. Retrieve object graph by whatever means (entity query or get)通过任何方式(实体查询或获取)检索 object 图
  2. Detach from hibernate session从 hibernate session 分离
  3. Store object graph in request.session将 object 图形存储在请求中。session
  4. Continue to modify object graph across multiple http requests跨多个 http 请求继续修改 object 图
  5. Upon completion of your user edits, re-attach object graph to hibernate session (merge)完成用户编辑后,将 object 图重新附加到 hibernate session(合并)
  6. Flush hibernate session and commit刷新 hibernate session 并提交

Is this what you are doing?这是你在做什么? Hard to tell from your post if you want to keep your object graph in the hibernate session across requests, or you want to get it in the HttpSession across requests.很难从您的帖子中看出您是否想将 object 图表保留在 hibernate session 跨请求中,或者您希望跨请求在 HttpSession 中获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM