1

I am trying to create a wizard-like registration where I have multiple pages with each one a form and a "save and continue" button. I want to save the data of each form in a hibernate session and save it on the DB only at the end of the registration when the user confirms. I am not an Hibernate expert but I understood that I need to create a "long session". How do I do that? Do I do that by leaving the transaction open? How do I retrieve the "dirty" entity from one request to the other ? Say we have an GWTP action handler that does something like this

//page1 action           
public myAction1Result execute(myAction1Action action,ExecutionContext context) throws ActionException {

    Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    session.getTransaction().begin();

    User entityUser = new User();
    entityUser.setName(action.getName());
    session.save(entityUser); 

    return new myAction1Result();
}

Until the transaction will be open the userEntity won't be saved in the DB and will be transient. How I can retrieve it in another actionHandler?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Blascone
  • 11
  • 2

1 Answers1

0

There are a lot of possible scenarios:

1. Your ActionHandler should be a (session) singleton - therefore you don't need a hibernate transaction. You can simply edit and hold the object in your ActionHandler instance. After the workflow is finished you can call a seperated action (e.g. RegisterUserWorkflowCommitAction) which injects the main ActionHandler and commits your edited object.

2. Use transaction.merge(user).

//...
@Singleton
public class RegisterUserWorkflowActionHandler //...
//...
transaction = session.beginTransaction();
session.merge(action.getUser());
transaction.commit();
//...

3. Hold the transaction using an field from your (singleton) ActionHandler.

//...
@Singleton
public class RegisterUserWorkflowActionHandler //...
//...
if(this.getOpenTransaction() == null) {
    this.setOpenTransaction(session.beginTransaction())
}
//...

4. May have a look at Errai JPA. Maybe it's a great alternative for you.

Community
  • 1
  • 1
Flori
  • 671
  • 7
  • 12