title: Hibernate get saved object by id before persisted to database
body: <p>I have a hibernate object that I'm persisting using session.saveOrUpdate without committing to the database which gives me the object id. I'm passing the object id to my interface which will later be used to rebuild the object on submit. When I submit the form, I'm no longer able to do a session query to retrieve object because the object technically doesn't exist in the database. </p>
  
  <p>Does anybody know how to retrieve that object? If I just create a new object, the database id's will increment out of control. Any thoughts?</p>
  
  <p>Question Edits and clarification. </p>
  
  <p>When the form is submitted and contains errors, I need to catch the errors and reload the page with the errors. The problem I've been having is I'm losing the non persisted data in my collections. My solution was to temporarily persist the current object with all the child objects to the users session and then copy the persisted object back into the main object once the page reloaded which would return all the users previous data. However I've run into the failed to lazily initialize a collection of role: which is why I've resorted to a transaction such as session.update. This solved this issue, but gave id's to the newly created objects prior to the validation error before actually saving to the database. </p>
  
  <p>Code Sample </p>
  
  <pre><code>@Property
  private PurchaseRequest pr;
  
  @Persist
  private PurchaseRequest prPersist;   
  
  Class&lt;?&gt; onActivate(Long prId) {
      if(request.isXHR()) {
          return null;
      }
  
      PurchaseRequest purchaseRequest = null;
  
      if(prPersist != null) {           
          session.update(prPersist);
          purchaseRequest = prPersist;
          prPersist = null;
      } else {
          purchaseRequest = (PurchaseRequest) session.createCriteria(PurchaseRequest.class)
              .add(Restrictions.idEq(prId))
              .uniqueResult();
      }
  
      this.pr = purchaseRequest;
  }
  </code></pre>
  
  <p>Validation Method</p>
  
  <pre><code>void onValidateFromPR() throws Exception {
      if (form.getHasErrors()) {
          prPersist = this.pr;
          return Page.Index;
      }
  }
  </code></pre>