title: Using Spring data JPA with MS SQL server
body: <p>I am setting up an app using Spring data JPA, Hibernate and MS SQL Server and unfortunately, I got a beat messed up with the configurations.</p>
  
  <p>I hope someone here code make things clearer:.</p>
  
  <p>This is my mvc-dispatcher.xml which is the application context:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans     
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/data/jpa 
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"&gt;
  
  &lt;context:component-scan base-package="com.yyy.yyy" /&gt;
  
  &lt;context:property-placeholder location="classpath:db.properties"/&gt;
  
  
  &lt;!--  DATA BASE --&gt;
  &lt;bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="generateDdl" value="true" /&gt;
              &lt;property name="database" value="SQL_SERVER" /&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="persistenceUnitName" value="punit" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClass}" /&gt; 
      &lt;property name="url" value="${jdbc.url}" /&gt; 
      &lt;property name="username" value="${jdbc.user}" /&gt; 
      &lt;property name="password" value="${jdbc.pwd}" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;jpa:repositories base-package="com.yyy.yyy.yyy.repository"/&gt;
  
  &lt;bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix"&gt;
          &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="suffix"&gt;
          &lt;value&gt;.jsp&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now, I also have a persistence.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"&gt;
      &lt;persistence-unit name="punit"&gt;     
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /&gt;
              &lt;property name="hibernate.connection.url" value="${jdbc.url}" /&gt;
              &lt;property name="hibernate.connection.driver_class" value="${jdbc.driverClass}" /&gt;
              &lt;property name="hibernate.connection.username" value="${jdbc.user}" /&gt;
              &lt;property name="hibernate.connection.password" value="${jdbc.pwd}" /&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="create-drop" /&gt; 
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I don't understand where should I put the DB connection data?
  And I also cant connect to the DB (I get connection refused) - I am getting exception at server startup.</p>
  
  <p>My project runs on Tomcat and I used <a href="https://github.com/SpringSource/spring-data-jpa-examples" rel="nofollow">https://github.com/SpringSource/spring-data-jpa-examples</a> as a template.</p>
  
  <p><strong>EDIT</strong>
  I managed to overcome the problems and publish to tomcat successfully,by removing the persistence XML file and use only the spring context file:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" 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.0.xsd         http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/data/jpa          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd         http://www.springframework.org/schema/tx          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
     &lt;context:component-scan base-package="yyy.yyy.yyy" /&gt;
     &lt;context:property-placeholder location="classpath:db.properties" /&gt;
     &lt;!-- DATA BASE --&gt;
     &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
        &lt;property name="packagesToScan" value="yyy.yyy.yyy.yyy.domain" /&gt;
        &lt;property name="jpaVendorAdapter"&gt;
           &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="generateDdl" value="false" /&gt;
              &lt;property name="showSql" value="true" /&gt;
              &lt;property name="database" value="SQL_SERVER" /&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" /&gt;
           &lt;/bean&gt;
        &lt;/property&gt;
     &lt;/bean&gt;
     &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
        &lt;property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" /&gt;
        &lt;property name="url" value="jdbc:jtds:sqlserver://localhost;databaseName=db" /&gt;
        &lt;property name="username" value="yyy" /&gt;
        &lt;property name="password" value="yyyyy" /&gt;
     &lt;/bean&gt;
     &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
        &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
        &lt;property name="dataSource" ref="dataSource" /&gt;
     &lt;/bean&gt;
     &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
     &lt;jpa:repositories base-package="yyy.yyy.yyy.yyy.repository" /&gt;
     &lt;!-- MVC --&gt;
     &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix"&gt;
           &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
        &lt;/property&gt;
        &lt;property name="suffix"&gt;
           &lt;value&gt;.jsp&lt;/value&gt;
        &lt;/property&gt;
     &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>The app now loads fine, but when I use my repository and call save, I get connection refused exception.</p>
  
  <p>Anyone knows why?</p>
  
  <p>Help please.
  Idob</p>