title: Hibernate and Spring - load operations do the trich, update/delete operations don't
body: <p>I've been following this tutorial:
  <a href="http://www.scribd.com/doc/25244173/Java-Struts-Spring-Hibernate-Tutorial" rel="nofollow">http://www.scribd.com/doc/25244173/Java-Struts-Spring-Hibernate-Tutorial</a>
  The setup (described ahead) was working fine with the tutorial files, but when I have made the changes - delete / update actions just don't happen. No errors or quirks, it just completely ignores me! As for retrieving data - everything works perfectly..</p>
  
  <p>Almost all the files from the tut are the same, with these differences; The tutorial uses a services file:</p>
  
  <p><strong>Services.java</strong></p>
  
  <pre><code>package services;
  
  import org.springframework.transaction.annotation.Transactional;
  import org.hibernate.SessionFactory;
  import org.hibernate.Session;
  import data.*;
  import java.util.List;
  
  // This class is the business services tier in the application.
  // @Transactional is needed so that a Hibernate transaction is set up,
  //  otherwise updates won't have an affect
  @Transactional
  public class Services {
      // So Spring can inject the session factory
      SessionFactory sessionFactory;
      public void setSessionFactory(SessionFactory value) {
          sessionFactory = value;
      }
  
      // Shortcut for sessionFactory.getCurrentSession()
      public Session sess() {
          return sessionFactory.getCurrentSession();
      }
  
      public Event getEventById(long id) {
          return (Event) sess().load(Event.class, id);
      }
  
      public Person getPersonById(long id) {
          return (Person) sess().load(Person.class, id);
      }
  
      public void deleteEventById(long id) {
          sess().delete(getEventById(id));
      }
  
      public void deletePersonById(long id) {
          sess().delete(getPersonById(id));
      }
  
      public void createEvent(String name) {
          Event theEvent = new Event();
          theEvent.setName(name);
          sess().save(theEvent);
      }
  
      public void createPerson(String name) {
          Person p = new Person();
          p.setName(name);
          sess().save(p);
      }
  
      @SuppressWarnings("unchecked")
      public List getEvents() {
          return sess().createQuery("from Event").list();
      }
  
      @SuppressWarnings("unchecked")
      public List getPeople() {
          return sess().createQuery("from Person").list();
      }
  
      public void removePersonFromEvent(int personId, int eventId) {
          getEventById(eventId).getPeople().remove(getPersonById(personId));
      }
  
      public void addPersonToEvent(int personId, int eventId) {
          getEventById(eventId).getPeople().add(getPersonById(personId));
      }
  }
  </code></pre>
  
  <p>and I have tried to separate the files by using a parent controller and static calls to HibernateUtil:
  <strong>HibernateUtil.java</strong></p>
  
  <pre><code>package com.epa.util;
  
  import org.hibernate.SessionFactory;
  import org.springframework.transaction.annotation.Transactional;
  @Transactional
  public class HibernateUtil {
  
      // So Spring can inject the session factory
      static SessionFactory sessionFactory;
      public void setSessionFactory(SessionFactory value) {
          sessionFactory = value;
      }
  
      // Shortcut for sessionFactory.getCurrentSession()
      public static SessionFactory getSessionFactory() {
          return sessionFactory;
      }
  }
  </code></pre>
  
  <p><strong>BaseController.java</strong></p>
  
  <pre><code>package com.epa.controller.base;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.epa.controller.EventController;
  import com.epa.controller.PersonController;
  import com.epa.util.HibernateUtil;
  
  @Transactional
  public class BaseController {
  
      protected SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      // Shortcut for sessionFactory.getCurrentSession()
      public Session sess() {
          return sessionFactory.getCurrentSession();
      }
  
      private PersonController personController = null;
      private EventController eventController = null;
  
      public PersonController getPersonController() {
          if (this.personController == null) {
              this.personController = new PersonController();
          }
          return personController;
      }
  
      public EventController getEventController() {
          if (this.eventController == null) {
              this.eventController = new EventController();
          }
          return eventController;
      }
  }
  </code></pre>
  
  <p><strong>EventController.java</strong></p>
  
  <pre><code>package com.epa.controller;
  
  import java.util.List;
  
  import org.springframework.transaction.annotation.Transactional;
  
  import com.epa.controller.base.BaseController;
  import com.epa.model.Event;
  
  @Transactional
  public class EventController extends BaseController {
  
      public Event getEventById(long id) {
          return (Event) sess().load(Event.class, id);
      }
  
      public void deleteEventById(long id) {
          sess().delete(getEventById(id));
      }
  
      public void createEvent(String name) {
          Event theEvent = new Event();
          theEvent.setName(name);
          sess().save(theEvent);
      }
  
      @SuppressWarnings("unchecked")
      public List getEvents() {
          return sess().createQuery("from Event").list();
      }
  
  }
  </code></pre>
  
  <p>and spring's <strong>applicationContext.xml</strong></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:aop="http://www.springframework.org/schema/aop"
         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/tx 
         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
         http://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"&gt;
  
      &lt;!-- The singleton hibernate session factory --&gt;
      &lt;bean id="sessionFactory" scope="singleton"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Spring's hibernate transaction manager, in charge of making hibernate sessions/txns --&gt;
      &lt;bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- So classes/functions with @Transactional get a hibernate txn --&gt;
      &lt;tx:annotation-driven /&gt;
  
      &lt;!-- Inject my business services class to the actions 
      &lt;bean id="services" class="com.epa.services.Services" scope="singleton"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;--&gt;
  
      &lt;!-- Inject my business services class to the actions --&gt;
      &lt;bean id="hibernateUtil" class="com.epa.util.HibernateUtil" scope="singleton"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt; 
  
      &lt;bean id="baseController" class="com.epa.controller.base.BaseController" scope="singleton" /&gt;
  
  &lt;/beans&gt;
  </code></pre>