title: Spring Data JPA Setup not working
body: <p>I made a simple java program with Spring Data JPA + Hibernate + Microsoft SQL Server. When I run the program I get the following exception and I can't figure out what the problem is.</p>
  
  <pre><code>Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
      at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431)
      at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:447)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:277)
      at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
      at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
      at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
      at com.sun.proxy.$Proxy32.findAll(Unknown Source)
      at test.Main.main(Main.java:13)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
  Caused by: java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode;
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.prepareFlushMode(HibernateJpaDialect.java:187)
      at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:173)
      at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380)
      ... 19 more
  </code></pre>
  
  <p>This is my main method:</p>
  
  <pre><code>package test;
  
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import java.util.*;
  
  public class Main {
      public static void main(String[] args) {
          AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Context.class);
  
          NameRepository repo = context.getBean(NameRepository.class);
  
          List&lt;Name&gt; names = repo.findAll();
      }
  }
  </code></pre>
  
  <p>This is my context config:</p>
  
  <pre><code>package test;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.jdbc.datasource.DriverManagerDataSource;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.sql.DataSource;
  import javax.persistence.EntityManagerFactory;
  
  import java.util.Properties;
  
  @Configuration
  @EnableJpaRepositories("test")
  @EnableTransactionManagement
  @ComponentScan("test")
  public class Context {
  
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
          dataSource.setUrl("jdbc:sqlserver://xxxxxxx;instanceName=xxxxxx;databaseName=Test");
          dataSource.setUsername("xxxxxxx");
          dataSource.setPassword("xxxxxxx");
  
          return dataSource;
      }
  
      @Bean
      public EntityManagerFactory entityManagerFactory() {
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  
          Properties jpaProperties = new Properties();
          jpaProperties.put("hibernate.dialect","org.hibernate.dialect.SQLServer2008Dialect");
  
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setDataSource(dataSource());
          factory.setPackagesToScan("test");
          factory.setJpaVendorAdapter(vendorAdapter);
          factory.setJpaProperties(jpaProperties);
          factory.setPersistenceUnitName("org.hibernate.jpa.HibernatePersistenceProvider");
          factory.afterPropertiesSet();
  
          return factory.getObject();
      }
  
      @Bean
      public JpaTransactionManager transactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(entityManagerFactory());
  
          return transactionManager;
      }
  }
  </code></pre>
  
  <p>My entity class:</p>
  
  <pre><code>package test;
  
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  
  @Entity
  public class Name {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String name;
  
      public Long getId() {
          return id;
      }
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
  
      public Name() {
  
      }
  }
  </code></pre>
  
  <p>My Repository interface:</p>
  
  <pre><code>package test;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.stereotype.Repository;
  
  @Repository
  public interface NameRepository extends JpaRepository&lt;Name, Long&gt; {
  }
  </code></pre>
  
  <p>And of course my maven dependencies:</p>
  
  <pre><code>&lt;dependencies&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
      &lt;version&gt;4.3.5.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
      &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
      &lt;version&gt;1.10.6.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
      &lt;version&gt;5.2.6.Final&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;com.microsoft.sqlserver&lt;/groupId&gt;
      &lt;artifactId&gt;mssql-jdbc&lt;/artifactId&gt;
      &lt;version&gt;6.1.0.jre8&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
      &lt;version&gt;4.3.5.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>Can anyone please help me?</p>