title: basic hibernate and spring mvc integration of view, dao, service and controller
body: <p><strong><code>Status:</code></strong> Thankful to the answers but No one has answered the Important tag description in reflection to my simple code given. (20 Jul 13)</p>
  
  <p>I have read many tutorials and yet every thing just mixes up or is only as a specific example that looks tiny or abstract.</p>
  
  <p>I really cannot make something get logical in my mind. May be I learn with specific actual code.</p>
  
  <p><strong>Question:</strong> Can anyone show how to make the following incomplete code <strong><code>work completely</code></strong> with <strong>spring 3.x and hibernate 4.x.</strong></p>
  
  <hr>
  
  <p><strong>Important:</strong> </p>
  
  <p>I would like, even in this simple example, 
  make <strong>sessionfactory</strong> and database quering by hibernate <strong>in the Service</strong> class (making boundry there as in large applications Service classes use many DAOs and commit transactions <strong>in one go</strong>)</p>
  
  <p>I forget where i read it, perhaps in the spring documentation - but it clearly said, don't put @Transactional on your DAOs :P
  so generally, your service tier is where you define your transaction boundaries.
  a service method is typically a chunk of things that if all pass, commit otherwise fail and rollback
  That may not be a diehard rule of course, but its how we architected our enterprise resource planning web core.
  I forget where i read it, perhaps in the spring documentation - but it clearly said, don't put @Transactional on your DAOs</p>
  
  <p>e.g  <a href="http://blog.patouchas.net/technology/hibernate-dao-java-tutorial/" rel="nofollow">http://blog.patouchas.net/technology/hibernate-dao-java-tutorial/</a> like without spring ?</p>
  
  <hr>
  
  <p>The <strong>annotations</strong> are missing or incorrect. What should be the correct annotations.</p>
  
  <p>Would there be any spring.xml (other than the usual) for these classes in specific? I would like to prefer only annotations if it works. correct annotations like @component , service, repository, resource, autowired</p>
  
  <p>This is how I get the transaction generally</p>
  
  <pre><code>   Configuration configuration = new Configuration();
     configuration.configure();
  
     ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                                          .applySettings(configuration.getProperties())
                                          .buildServiceRegistry();
  
     SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
     Session session = sessionFactory.openSession();
     session.beginTransaction();
     session.getTransaction().commit();
     session.close();
  </code></pre>
  
  <p>now spring and hibernate</p>
  
  <pre><code>@Controller
  public class UserController {
      private IUserService userService;
  
      @RequestMapping("/users")
      public String creatUser(){
          Users user = new Users();
          user.setEmail("myemail@mydomain.com");
          user.setName("myname");
          userService.creatUser(user);
          return "user-creation-result";      
      }
  }
  
  public class UserService implements IUserService{
      private IUserDAO userDAO;
      public void creatUser(Users user){
          //what to do here
          //how to call the sessionfactory
          //and call it in a way that each call
          // gives the same instance
           userDAO.creatUser(user);        
      }
  }
  
  public class UserDAO implements IUserDAO{
      public void creatUser(Users user){
          // what to do here?
      }
  }
  </code></pre>
  
  <p>this wont be so important though.</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  
  &lt;hibernate-configuration&gt;
  
      &lt;session-factory&gt;
  
          &lt;!-- Database connection settings --&gt;
          &lt;property name="connection.driver_class"&gt;org.postgresql.Driver&lt;/property&gt;
          &lt;property name="connection.url"&gt;jdbc:postgresql://localhost:5432/postgres&lt;/property&gt;
          &lt;property name="connection.username"&gt;postgres&lt;/property&gt;
          &lt;property name="connection.password"&gt;abc&lt;/property&gt;
  
          &lt;!-- JDBC connection pool (use the built-in) --&gt;
          &lt;property name="connection.pool_size"&gt;1&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.PostgreSQLDialect&lt;/property&gt;
  
          &lt;!-- Disable the second-level cache  --&gt;
          &lt;property name="cache.provider_class"&gt;org.hibernate.cache.internal.NoCacheProvider&lt;/property&gt;
  
          &lt;!-- Echo all executed SQL to stdout --&gt;
          &lt;property name="show_sql"&gt;true&lt;/property&gt;
  
          &lt;!-- Drop and re-create the database schema on startup --&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
  
          &lt;!-- Names the annotated entity class --&gt;
  
          &lt;mapping class="taskmanagsetup.Boards" /&gt;
          &lt;mapping class="taskmanagsetup.BoardPrivileges" /&gt;
          &lt;mapping class="taskmanagsetup.Boxes" /&gt;
          &lt;mapping class="taskmanagsetup.BoxPrivileges" /&gt;
          &lt;mapping class="taskmanagsetup.Groups" /&gt;
          &lt;mapping class="taskmanagsetup.Tasks" /&gt;
          &lt;mapping class="taskmanagsetup.TaskPrivileges" /&gt;
          &lt;mapping class="taskmanagsetup.Users" /&gt;
  
      &lt;/session-factory&gt;
  
  &lt;/hibernate-configuration&gt;
  
  
  
  @Entity
  public class Users {
      @Id
      @GeneratedValue
      private long id;
      @ManyToMany
      private Collection&lt;Groups&gt; groupList = new ArrayList&lt;Groups&gt;();
      private String type; // admin / teamlead / normal
      private String name;
      private String email;
      private String password;
      @Lob
      private String description;
      private boolean isEnabled;
  
      /**
       * @return the id
       */
      public long getId() {
          return id;
      }
  
      /**
       * @param id the id to set
       */
      public void setId(long id) {
          this.id = id;
      }
  
      /**
       * @return the groupdId
       */
  
  
      /**
       * @param groupdId the groupdId to set
       */
  
  
      /**
       * @return the type
       */
      public String getType() {
          return type;
      }
  
      /**
       * @param type the type to set
       */
      public void setType(String type) {
          this.type = type;
      }
  
      /**
       * @return the name
       */
      public String getName() {
          return name;
      }
  
      /**
       * @param name the name to set
       */
      public void setName(String name) {
          this.name = name;
      }
  
      /**
       * @return the email
       */
      public String getEmail() {
          return email;
      }
  
      /**
       * @param email the email to set
       */
      public void setEmail(String email) {
          this.email = email;
      }
  
      /**
       * @return the password
       */
      public String getPassword() {
          return password;
      }
  
      /**
       * @param password the password to set
       */
      public void setPassword(String password) {
          this.password = password;
      }
  
      /**
       * @return the isEnabled
       */
      public boolean isIsEnabled() {
          return isEnabled;
      }
  
      /**
       * @param isEnabled the isEnabled to set
       */
      public void setIsEnabled(boolean isEnabled) {
          this.isEnabled = isEnabled;
      }
  
      /**
       * @return the groupList
       */
      public Collection&lt;Groups&gt; getGroupList() {
          return groupList;
      }
  
      /**
       * @param groupList the groupList to set
       */
      public void setGroupList(Collection&lt;Groups&gt; groupList) {
          this.groupList = groupList;
      }
  
      /**
       * @return the description
       */
      public String getDescription() {
          return description;
      }
  
      /**
       * @param description the description to set
       */
      public void setDescription(String description) {
          this.description = description;
      }
  }
  </code></pre>