title: Java spring + hibernate interface (DAO) <bean>
body: <p>This is a very simple spring + hibernate example. What am I doing wrong here? </p>
  
  <p>DTO:</p>
  
  <pre><code>package com.xx.dto;
  
  import java.util.Date;
  import javax.persistence.*;
  
  @Entity
  @Table(name = "users")
  public class UserData {
  
      @Id
      @GeneratedValue
      @Column(name = "id")
      private Integer id;
  
      @Column(name = "user_firstname")
      private String firstname;
  
      @Column(name = "user_lastname")
      private String lastname;
  
      @Column(name = "user_sex")
      private String sex;
  
      @Column(name = "user_birthdate")
      private Date birthDate;
  
      @Column(name = "user_email")
      private String email;
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getFirstname() {
          return firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      public String getSex() {
          return sex;
      }
  
      public void setSex(String sex) {
          this.sex = sex;
      }
  
      public Date getBirthDate() {
          return birthDate;
      }
  
      public void setBirthDate(Date birthDate) {
          this.birthDate = birthDate;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  }
  </code></pre>
  
  <p>DAO interface:</p>
  
  <pre><code>package com.xx.dao;
  
  import java.util.List;
  import com.provisori.dto.UserData;
  
  public interface UserDataDao {
  
      void saveUser(UserData user);
  
      void deleteUser(String key);
  
      void updateUser(UserData user);
  
      List&lt;UserData&gt; listUser();
  }
  </code></pre>
  
  <p>DAO implementation:</p>
  
  <pre><code>package com.xx.imp;
  
  import java.util.List;
  import org.hibernate.Criteria;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.criterion.Restrictions;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  import com.provisori.dao.UserDataDao;
  import com.provisori.dto.UserData;
  
  public class UserDataImp implements UserDataDao{
  
      @Autowired
      SessionFactory sessionFactory;
  
      @Transactional
      @Override
      public void saveUser(UserData user) {
          sessionFactory.getCurrentSession().save(user);
      }
  
      @Transactional
      @SuppressWarnings("unchecked")
      @Override
      public List&lt;UserData&gt; listUser() {
          return sessionFactory.getCurrentSession()
                  .createCriteria(UserData.class).list();
      }
  
      @Transactional
      @Override
      public void updateUser(UserData user) {
          sessionFactory.getCurrentSession().update(user);
  
      }
  
      @Transactional
      public UserData getUser(String key) {
          Session session = sessionFactory.getCurrentSession();
          Criteria criteria = session.createCriteria(UserData.class);
          criteria.add(Restrictions.eq("key", key));
          return (UserData) criteria.uniqueResult();
      }
  
      @Transactional
      @Override
      public void deleteUser(String key) {
          UserData user = getUser(key);
          sessionFactory.getCurrentSession().delete(user);
      }
  }
  </code></pre>
  
  <p>spring.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"
      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.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt;
  
      &lt;!-- Needed for Autowiring --&gt;
      &lt;context:annotation-config /&gt;
  
      &lt;!-- MySQL DataSource --&gt;
      &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
          &lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt;
          &lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/xx" /&gt;
          &lt;property name="user" value="root" /&gt;
          &lt;property name="password" value="" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Hibernate SessionFactory --&gt;
      &lt;bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="annotatedClasses"&gt;
              &lt;list&gt;
                  &lt;value&gt;com.provisori.dto.UserData&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;value&gt;
                  hibernate.hbm2ddl.auto=update
                  hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
              &lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;!-- Transaction Management --&gt;
      &lt;tx:annotation-driven transaction-manager="txManager" /&gt;
      &lt;bean id="txManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
      &lt;/bean&gt;
  
      &lt;!-- Java Bean --&gt;
      &lt;bean id="userDataDao" class="com.provisori.imp.UserDataImp" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Main.class</p>
  
  <pre><code>package com.provisori.dto;
  import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  import com.provisori.dao.UserDataDao;
  import com.provisori.imp.UserDataImp;
  
  public class TestMain {
  
     public static void main(String[] args) {
  
        // Construct the spring application context
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  
        // Register hook to shutdown Spring gracefully
        // See http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-shutdown
        context.registerShutdownHook();
  
        // Get the business bean from context
        UserDataDao dao = (UserDataImp) context.getBean("userDataDao");
  
        // Create simple property objects
        UserData user = new UserData();
        user.setFirstname("firstnameTest");
        user.setLastname("lastnameTest");
        dao.saveUser(user);
     }
  }
  </code></pre>
  
  <p>result</p>
  
  <p>Exception in thread "main" java.lang.ClassCastException: $Proxy13 cannot be cast to com.provisori.imp.UserDataImp
      at com.provisori.dto.TestMain.main(TestMain.java:21)</p>