abden003
abden003

Reputation: 1335

Save changes to database vaadin

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!

Upvotes: 0

Views: 3666

Answers (2)

abden003
abden003

Reputation: 1335

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;
}

Upvotes: 0

Milan Baran
Milan Baran

Reputation: 4232

Well, the first thing you need to realize is Vaadin differs from conventional request/response web framework. Actually, Vaadin is *event driven* framework very similar to Swing. It builds a application context from very first click of the user and holds it during whole website visit. The problem is there is no entry request point you can start hibernate session and no response point to close. There are tons of requests during a single click on button.

So, entitymanager-per-request pattern is completely useless. It is better to use one standalone em or em-per-session pattern with hibernate.connection_release after_transaction to keep connection pool low.

To the JPAContianer, it is not usable as far you need to refresh the container or you have to handle beans with relations. Also, I did not manage to get it working with batch load, so every reading of entry or relation equals one select to DB. Do not support lazy loading.

All you need is open EM/session. Try to use suggested patters or open EM/session every transaction and merge your bean first.

Your question is quite complex and hard to answer, but I hope these links help you get into:

Pojo binding strategy for hibernate

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

MVP-lite

https://vaadin.com/directory#addon/mvp-lite (stick with event driven pattern)

Upvotes: 4

Related Questions