title: Spring with hibernate dont flush the session after exception error
body: <p>I am using Spring transaction management with Hibernate. My service layer methods are transactional. This error is occurring when am testing a use case when user is trying to create a company with an already existing ID. Here is my controller:</p>
  
  <pre><code>@RequestMapping(value = "/CreateCompany", method = RequestMethod.POST)
  public @ResponseBody
  String createUser(HttpServletRequest request) {
      String response = null;
      try {
  
          response = getUserServiceTransaction().save(request);
  
      } catch (Exception e) {
          if (logger.isErrorEnabled()) {
              logger.error(
                      "ERROR: service URL /CreateCompany; " +
                      "method createUser(..)"+ 
                      e.getMessage());
          }
      }
      return response;
  }
  </code></pre>
  
  <p>Service save method:</p>
  
  <p>@Override
      public String save(HttpServletRequest request) throws ServiceException {</p>
  
  <pre><code>    String requestJson = null;
      try {
          requestJson = getUtils().fetchJsonRequestString(request);
          Company company = (Company) getUtils().fromJson(requestJson,
                  Company.class);
  
          String companyId = company.getCompanyId();
          getCompanyDao().save(company);
  
          List&lt;String&gt; linkedCompanies = null;
  
          String parentCompanyId = company.getCompanyId();
  
          linkedCompanies = company.getLinkedCompanies();
  
          Iterator&lt;String&gt; iterator = linkedCompanies.iterator();
  
          while (iterator.hasNext()) {
              CompanyLinkage companyLinkage = new CompanyLinkage();
              companyLinkage.setParentCompanyId(parentCompanyId);
              companyLinkage.setChildCompanyId(iterator.next());
              getCompanyLinkageDao().save(companyLinkage);
          }
  
          requestJson = getUtils().toKendoResponse(true);
      }
      catch (Exception e) {
          logger.error("Error:" + e);
          requestJson = getUtils().toKendoResponse(false);
      }
      return requestJson;
  
  }
  </code></pre>
  
  <p>My DAO:</p>
  
  <p>public class CompanyDaoImpl extends HibernateDaoSupport implements CompanyDao{</p>
  
  <pre><code>@Override
  public void save(Company company) throws DAOException {
      try{
          getHibernateTemplate().save(company);
          getHibernateTemplate().flush();
      }
      catch(Exception e){
          throw new DAOException(e);
      }
  
  }
  </code></pre>
  
  <p>Transactional beans:</p>
  
  <pre><code>&lt;!-- User Service  --&gt;
  &lt;bean id="userService" class="com.ig.avs.adapters.db.service.UserService"&gt;
      &lt;property name="companyDao" ref="companyDao"/&gt;
      &lt;property name="companyLinkageDao" ref="companyLinkageDao"/&gt;
      &lt;property name="utils" ref="utils" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="userServiceTransaction"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;
  &lt;property name="transactionManager" ref="txManager" /&gt;
  &lt;property name="target" ref="userService" /&gt;
  &lt;property name="transactionAttributes"&gt;
  
  &lt;props&gt;
  &lt;prop key="save*"&gt;PROPAGATION_REQUIRED,-java.lang.Exception&lt;/prop&gt;
  &lt;prop key="update*"&gt;PROPAGATION_REQUIRED,-java.lang.Exception&lt;/prop&gt;
  &lt;prop key="delete*"&gt;PROPAGATION_REQUIRED,-java.lang.Exception&lt;/prop&gt;
  &lt;prop key="get*"&gt;PROPAGATION_SUPPORTS,readOnly , -java.lang.Exception&lt;/prop&gt;
  &lt;prop key="find*"&gt;PROPAGATION_SUPPORTS,readOnly ,-java.lang.Exception&lt;/prop&gt;
  &lt;prop key="list*"&gt;PROPAGATION_SUPPORTS, readOnly ,-java.lang.Exception&lt;/prop&gt;
  &lt;/props&gt;
  </code></pre>
  
  <p>In my JSP am checking that if my response is d:false ( which I am creating in getUtils().toKendoResponse(false) ) then I display an alert to the user "There is some issue in registration"</p>
  
  <p>However once the error comes during save in the service layer (duplicate key error) , the code reaches the end of the service. After that another error occurs in the controller:</p>
  
  <p>ERROR CompanyController:47 - ERROR: service URL /CreateCompany; method createUser(..)null id in com.ig.avs.common.entity.db.Company entry (don't flush the Session after an exception occurs)</p>
  
  <p>And the controller returns 'response' as null. </p>
  
  <p>I have tried removing the flush I am doing in my dao layer but it didnt help. I have looked at other answers on the site regarding this but in most of them people were not returning back after an error. I am catching my exception and returning from my service so not sure where another flush is occurring.</p>
  
  <p>Any input at all would be a great help!</p>