title: inject entitymanager using spring+jpa
body: <p>How to inject EntityManager object using spring+JPA</p>
  
  <p>I am using spring-context </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:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
          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-2.0.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:component-scan base-package="com.javapapers.spring.mvc" /&gt;
      &lt;context:component-scan base-package="com.cnsi.dao.Impl" /&gt;
  
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;bean
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/view/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
      &lt;!-- &lt;jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/spitterPU" 
          /&gt; --&gt;
  
      &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"&gt;
          &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
          &lt;property name="url" value="jdbc:oracle:thin:@10.86.24.7:1521:wahipaaD" /&gt;
          &lt;property name="username" value="mddemo" /&gt;
          &lt;property name="password" value="mddemo" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaVendorAdapter"
          class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="database" value="ORACLE" /&gt;
          &lt;property name="showSql" value="true" /&gt;
          &lt;property name="generateDdl" value="false" /&gt;
          &lt;property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" /&gt;
      &lt;/bean&gt;
  
  
      &lt;bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceXmlLocation" value="classpath:META-INF/ spring-persistence.xml" /&gt;
          &lt;property name="persistenceUnitName" value="personPersistenceUnit" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;!-- &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt; --&gt;
          &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
      &lt;/bean&gt;
      &lt;!-- &lt;bean class="com.cnsi.dao.Impl.JpaUserDao" id="jpaUserDao"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt; --&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>And my dao is, inject EntityManager</p>
  
  <pre><code>package com.cnsi.dao.Impl;
  
  import java.util.List;
  
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Repository;
  
  import com.cnsi.dao.UserDao;
  import com.cnsi.modal.User;
  
  @Repository
  public class JpaUserDaoImpl implements UserDao {
  
      @Autowired
      EntityManagerFactory entityManagerFactory;
  
      EntityManager em = entityManagerFactory.createEntityManager();
  
      @Override
      public void add(User u) {
          em.persist(u);
      }
  
      @Override
      public void edit(User u) {
          em.merge(u);
  
      }
  
      @Override
      public void delete(int id) {
  
          em.remove(getInterfaceId(id));
  
      }
  
      @Override
      public User getInterfaceId(int id) {
  
          return (User) em.find(User.class, id);
      }
  
      @Override
      public List&lt;User&gt; getAllInterface() {
  
          return em.createQuery("from user").getResultList();
      }
  
  }
  </code></pre>
  
  <p>my persistence.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence xmlns="http://java.sun.com/xml/ns/persistence"
      version="1.0"&gt;
      &lt;persistence-unit name="personPersistenceUnit" transaction-type="RESOURCE_LOCAL" &gt;
      &lt;class&gt;com.cnsi.modal.User&lt;/class&gt;
  &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>So I am getting below error:</p>
  
  <pre><code>java.lang.NoSuchMethodError: org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.getJpaVendorAdapter()Lorg/springframework/orm/jpa/JpaVendorAdapter;
  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:240)
  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:232)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
  at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFacto
  </code></pre>