保存对数据库vaadin的更改 [英] Save changes to database vaadin
问题描述
我有一个包含表格的应用程序,当您点击表格中的一个项目时,它会用数据填充一组文本框(FieldGroup),然后您可以选择保存更改我是想知道我将如何保存用户对我的postgres数据库所做的更改。我正在为此应用程序使用vaadin和hibernate。到目前为止,我尝试过在用户单击保存按钮
I have an application which has a table and when you click on an item in the table it fills in a group of textfields with its data (FieldGroup), and then you have the option of saving the changes I was wondering how would I save the changes the user makes to my postgres database. I am using vaadin and hibernate for this application. So far I have tried to do
editorField.commit() // after the user clicks the save button
我曾尝试
I have tried
editorField.commit()
hbsession.persist(editorField) //hbsession is the name of my Session
而且我也试过了
and I have also tried
editorField.commit();
hbsession.save(editorField);
最后两个给我以下错误:
The last two ones give me the following error
Caused by: org.hibernate.SessionException: Session is closed!
推荐答案
我已经想出了如何更改数据库这里有一些代码来演示:
I have figured out how to make changes to the database here is some code to demonstrate:
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;
}
这篇关于保存对数据库vaadin的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!