title: HibernateException: save is not valid without active transaction
body: <p>So I thoroughly went through 
  <a href="https://stackoverflow.com/questions/11311075/get-is-not-valid-without-active-transaction">Get is not valid without active transaction</a>
  and know I don't have the problem of not autowiring the SessionFactory and have followed best practices but still am encountering this exception:</p>
  
  <pre><code>org.hibernate.HibernateException: save is not valid without active transaction
      at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
      at com.sun.proxy.$Proxy31.save(Unknown Source)
      at com.bms.dao.DemandManageDAOImpl.save(DemandManageDAOImpl.java:25)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy29.save(Unknown Source)
      at com.bms.service.DemandServiceImpl.createDemand(DemandServiceImpl.java:26)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
      at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
      at com.sun.proxy.$Proxy30.createDemand(Unknown Source)
      at com.bms.dao.DemandManageDAO_UT.testCreateDemand(DemandManageDAO_UT.java:69)
  </code></pre>
  
  <p>Which I will try to include all the necessary config and code which is spread over multiple files. Sorry for all the files and thank you in advance. 
  This is Java 1.7, Spring 4.0.2.RELEASE, Hibernate 4.3.5.Final</p>
  
  <p>Seems like this was routine with previous versions. I can get around this by getting a session and beginning a transaction but I don't want to do that. </p>
  
  <p>context:
  applicationContext.xml </p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="
          http://www.springframework.org/schema/beans     
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
      &lt;!-- Database Configuration --&gt;
      &lt;import resource="classpath:database/hibernate.xml"/&gt;
  
      &lt;!-- Auto scan the components --&gt;
      &lt;context:component-scan base-package="com.bms" /&gt;
  
      &lt;tx:annotation-driven/&gt;
      &lt;bean id="demandManageDao" class="com.bms.dao.DemandManageDAOImpl"&gt;&lt;/bean&gt;
      &lt;bean id="demandService" class="com.bms.service.DemandServiceImpl"&gt; &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>hibernate.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
      &lt;import resource="classpath:database/dataSource.xml" /&gt;
  
      &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
          &lt;property name="location"&gt;
              &lt;value&gt;database/database.${demandEnv}.properties&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;!-- Hibernate Session Factory --&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.bms.demand.domain.data&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${jdbc.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.autocommit"&gt;true&lt;/prop&gt;
                  &lt;prop key="hibernate.current_session_context_class"&gt;thread&lt;/prop&gt; 
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt; 
  &lt;/beans&gt;
  </code></pre>
  
  <p>datasource.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"&gt;
  
  &lt;bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="location"&gt;
              &lt;value&gt;database/database.${demandEnv}.properties&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
   &lt;!-- HSQLDB Data Source --&gt;        
  
    &lt;bean id="dataSource" 
           class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName}" /&gt;
      &lt;property name="url" value="${jdbc.url}" /&gt;
      &lt;property name="username" value="${jdbc.username}" /&gt;
      &lt;property name="password" value="${jdbc.password}" /&gt;
  &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>test file:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
  @TransactionConfiguration(transactionManager = "transactionManager",defaultRollback=true)
  @Transactional
  public class DemandManageDAO_UT {
  
      private static ApplicationContext context;
      private SessionFactory sessionFactory;
      private DemandManageDAO demandManageDAO;
      private DemandService demandService;
  
      @BeforeClass
      public static void setUpClass(){
          context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
      }
  
      @Before
      public void setUp() throws Exception {
          demandManageDAO = (DemandManageDAO)context.getBean("demandManageDao");
          demandService = (DemandService)context.getBean("demandService");
          sessionFactory = (SessionFactory)context.getBean("sessionFactory");
      }
  
      @After
      public void tearDown() throws Exception {
      }
  
      @Test
      public void testCreateDemand() throws Exception {
          Demand myDemand2 = new Demand();
          myDemand2.setId(10L);
          myDemand2.setDescription("text");
          myDemand2.setMonths(1);
          Calendar calendar = Calendar.getInstance();
          calendar.set(2000, 7, 4);
          myDemand2.setStartDate(calendar.getTime());
          Availibility availibility = new Availibility();
          availibility.setId(2L);
          availibility.setMonth(calendar.getTime());
          availibility.setPercentage(new Integer(50));
          availibility.setResourceName("text");
          myDemand2.addAvailibility(availibility);
          Long perId = demandService.createDemand(myDemand2);
          assertThat(perId,is(myDemand2.getId()));
          Session session = sessionFactory.getCurrentSession();
          session.beginTransaction();
          Availibility sessionObject = (Availibility) session.get(Availibility.class, 2L);
          assertThat(sessionObject.getResourceName(),is("text"));
      }
  }
  </code></pre>
  
  <p>serviceImpl</p>
  
  <pre><code>@Component
  public class DemandServiceImpl implements DemandService {
  
      @Autowired
      DemandManageDAO demandManageDao;
  
      public void setDemandManageDao(DemandManageDAO demandManageDAO) {
          this.demandManageDao = demandManageDAO;
      }
  
      @Override
      @Transactional
      public Long createDemand(Demand myDemand2) throws Exception {
          return demandManageDao.save(myDemand2);
      }
  }
  </code></pre>
  
  <p>the DAO </p>
  
  <pre><code>@Repository
  public class DemandManageDAOImpl implements DemandManageDAO {
      @Autowired
      SessionFactory sessionFactory;
  
      public SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  
      @Override
      public Long save(Demand myDemand) throws Exception {
          return (Long) getCurrectSession().save(myDemand);   
      }
  
      private Session getCurrectSession() {
          return getSessionFactory().getCurrentSession();
      }
  }
  </code></pre>