title: Error when trying to create transactionManager bean in a Spring - Hibernate application
body: <p>I am getting an error from spring when it tries to create a transaction manager bean.
  I am posting my java classes and configuration files below.</p>
  
  <p>foo.bar/HelloApp.java :</p>
  
  <pre><code>package foo.bar;
  
  import com.mkyong.stock.bo.StockBo;
  import com.mkyong.stock.model.StockEntity;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.support.ClassPathXmlApplicationContext;
  
  public class HelloApp {
      public static void main(String[] args) {
          ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  
          StockBo stockBo = (StockBo)context.getBean("stockBo");
  
          //insert
          StockEntity stock = new StockEntity();
          stock.setStockCode("7668");
          stock.setStockName("HAIO");
          stockBo.save(stock);
  
          //select
          StockEntity stock2 = stockBo.findByStockCode("7668");
          System.out.println(stock2);
  
          //update
          //stock2.setStockName("HAIO-1");
          //stockBo.update(stock2);
  
          //delete
          //stockBo.delete(stock2);
  
          System.out.println("Done");
      }
  }
  </code></pre>
  
  <p>com.mkyong.stock.bo/StockBoImpl.java :</p>
  
  <pre><code>package com.mkyong.stock.bo;
  
  import com.mkyong.stock.dao.StockDao;
  import com.mkyong.stock.model.StockEntity;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Propagation;
  import org.springframework.transaction.annotation.Transactional;
  
  @Service("stockBo")
  @Transactional(propagation = Propagation.SUPPORTS)
  public class StockBoImpl implements StockBo{
  
      @Autowired
      StockDao stockDao;
  
      public void setStockDao(StockDao stockDao)
      {
          this.stockDao = stockDao;
      }
  
      @Override
      public void save(StockEntity stock) {
          stockDao.save(stock);
      }
  
      @Override
      public void update(StockEntity stock) {
          stockDao.save(stock);
      }
  
      @Override
      public void delete(StockEntity stock) {
          stockDao.delete(stock);
      }
  
      @Override
      public StockEntity findByStockCode(String stockCode) {
          return stockDao.findByStockCode(stockCode);
      }
  }
  </code></pre>
  
  <p>com.mkyong.stock.dao/StockDaoImpl.java :</p>
  
  <pre><code>package com.mkyong.stock.dao;
  
  import com.mkyong.stock.model.StockEntity;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  
  @Repository("stockDao")
  public class StockDaoImpl implements StockDao{
  
      private SessionFactory sessionFactory;
  
      @Autowired
      public StockDaoImpl(SessionFactory sessionFactory)
      {
          this.sessionFactory = sessionFactory;
      }
  
      private Session currentSession()
      {
          return sessionFactory.getCurrentSession();
      }
  
      @Override
      public void save(StockEntity stock) {
          currentSession().save(stock);
      }
  
      @Override
      public void update(StockEntity stock) {
          currentSession().update(stock);
      }
  
      @Override
      public void delete(StockEntity stock) {
          currentSession().delete(stock);
      }
  
      @Override
      public StockEntity findByStockCode(String stockCode) {
          return (StockEntity)currentSession().get(StockEntity.class, stockCode);
      }
  }
  </code></pre>
  
  <p>com.mkyong.stock.model/StockEntity.java :</p>
  
  <pre><code>package com.mkyong.stock.model;
  
  import javax.persistence.*;
  
  import java.io.Serializable;
  
  import static javax.persistence.GenerationType.IDENTITY;
  
  
  @javax.persistence.Table(name = "stock", schema = "", catalog = "mkyong",
          uniqueConstraints = {
                  @UniqueConstraint(columnNames = "STOCK_NAME"),
                  @UniqueConstraint(columnNames = "STOCK_CODE")
          }
  )
  @Entity
  public class StockEntity implements Serializable{
      private int stockId;
      private String stockCode;
      private String stockName;
  
      //---constructors
      public StockEntity() {
      }
  
      public StockEntity(String stockCode, String stockName) {
          this.stockCode = stockCode;
          this.stockName = stockName;
      }
  
      //---getters and setters
      @javax.persistence.Column(name = "STOCK_ID", unique = true, nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
      @Id
      @GeneratedValue(strategy = IDENTITY)
      public int getStockId() {
          return stockId;
      }
  
      public void setStockId(int stockId) {
          this.stockId = stockId;
      }
  
  
  
      @javax.persistence.Column(name = "STOCK_CODE", unique = true, nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
      @Basic
      public String getStockCode() {
          return stockCode;
      }
  
      public void setStockCode(String stockCode) {
          this.stockCode = stockCode;
      }
  
  
  
      @javax.persistence.Column(name = "STOCK_NAME", unique = true, nullable = false, insertable = true, updatable = true, length = 20, precision = 0)
      @Basic
      public String getStockName() {
          return stockName;
      }
  
      public void setStockName(String stockName) {
          this.stockName = stockName;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (o == null || getClass() != o.getClass()) return false;
  
          StockEntity that = (StockEntity) o;
  
          if (stockId != that.stockId) return false;
          if (stockCode != null ? !stockCode.equals(that.stockCode) : that.stockCode != null) return false;
          if (stockName != null ? !stockName.equals(that.stockName) : that.stockName != null) return false;
  
          return true;
      }
  
      @Override
      public int hashCode() {
          int result = stockId;
          result = 31 * result + (stockCode != null ? stockCode.hashCode() : 0);
          result = 31 * result + (stockName != null ? stockName.hashCode() : 0);
          return result;
      }
  
      @Override
      public String toString()
      {
          return "Stock [stockCode=" + stockCode + ", stockId=" + stockId + ", stockName=" + stockName + "]";
      }
  }
  </code></pre>
  
  <p>resources/hibernate.cfg.xml :</p>
  
  <pre><code>&lt;?xml version='1.0' encoding='utf-8'?&gt;
  &lt;!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"&gt;
  &lt;hibernate-configuration&gt;
      &lt;session-factory&gt;
          &lt;property name="connection.url"&gt;jdbc:mysql://localhost:3306/mkyong&lt;/property&gt;
          &lt;property name="connection.driver_class"&gt;com.mysql.jdbc.Driver&lt;/property&gt;
          &lt;property name="connection.username"&gt;root&lt;/property&gt;
          &lt;property name="connection.password"&gt;mypassword&lt;/property&gt;
  
          &lt;property name="hibernate.current_session_context_class"&gt;thread&lt;/property&gt;
  
          &lt;!-- DB schema will be updated if needed --&gt;
          &lt;property name="hbm2ddl.auto"&gt;create&lt;/property&gt;
  
          &lt;!-- SQL dialect --&gt;
          &lt;property name="dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/property&gt;
  
          &lt;mapping class="com.mkyong.stock.model.StockEntity"/&gt;
          &lt;mapping resource="mapping.xml"/&gt;
      &lt;/session-factory&gt;
  &lt;/hibernate-configuration&gt;
  </code></pre>
  
  <p>resources/spring-config.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"&gt;
  
      &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;property name="configLocation" value="hibernate.cfg.xml"/&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;context:annotation-config /&gt;
      &lt;context:component-scan base-package="foo.bar, com.mkyong.stock"/&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And here is the error stack trace I get :</p>
  
  <pre><code>Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [spring-config.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:83)
      at foo.bar.HelloApp.main(HelloApp.java:10)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
  Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
      at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:85)
      at org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:91)
      at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:252)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
      ... 17 more
  </code></pre>