保存对数据库vaadin的更改

我有一个有一个表的应用程序,当你点击表中的一个项目时,它会填充一组带有数据的文本字段(FieldGroup),然后你可以选择保存更改我想知道如何保存将用户更改为我的postgres数据库 。 我在这个应用程序中使用vaadin和hibernate。 到目前为止,我已经尝试过

editorField.commit() // after the user clicks the save button 

我试过了

  editorField.commit() hbsession.persist(editorField) //hbsession is the name of my Session 

而我也试过了

  editorField.commit(); hbsession.save(editorField); 

最后两个给我以下错误

 Caused by: org.hibernate.SessionException: Session is closed! 

嗯,你需要意识到的第一件事是Vaadin与传统的请求/响应Web框架不同。 实际上, Vaadin是* 事件驱动 *框架,非常类似于Swing。 它从用户的第一次点击构建应用程序上下文,并在整个网站访问期间保留它。 问题是没有入口请求点你可以启动hibernate会话并且没有响应点关闭。 单击按钮时会有大量请求。

因此, entitymanager-per-request模式完全没用。 最好使用一个独立的em或em-per-session模式与hibernate.connection_release after_transaction来保持连接池较低。

对于JPAContianer,它不可用,因为您需要刷新容器或者您必须处理具有关系的bean。 此外,我没有设法让它与批量加载一起工作,因此每个条目或关系的读数等于一个选择到DB。 不支持延迟加载。

您只需要打开EM /会话。 尝试使用建议的模式或每个事务打开EM /会话并首先合并您的bean。

您的问题非常复杂且难以回答,但我希望这些链接可以帮助您进入:

用于hibernate的Pojo绑定策略

https://vaadin.com/forum#!/thread/39712

MVP-精简版

https://vaadin.com/directory#addon/mvp-lite (坚持事件驱动模式)

我已经想出如何更改数据库这里有一些代码来演示:

 try { /** define the session and begin it **/ hbsession = HibernateUtil.getSessionFactory().getCurrentSession(); hbsession.beginTransaction(); /** table is the name of the Bean class linked to the corresponding SQL table **/ String query = "UPDATE table SET name = " + textfield.getValue(); /** Run the string as an SQL query **/ Query q = hbsession.createQuery(query); q.executeUpdate(); /** This command saves changes or deletes a entry in table **/ hbsession.getTransaction().commit(); } catch (RuntimeException rex) { hbsession.getTransaction().rollback(); throw rex; }