title: Spring Data JPA : class path resource [] cannot be resolved to URL because it does not exist
body: <p>I'm kinda new to Spring but I decided to use Spring-Data-JPA for a personal project.
  I started following <a href="http://spring.io/guides/gs/accessing-data-jpa/" rel="nofollow noreferrer">"Accessing Data with JPA"</a> tutorial but with my own modification. (as you can see below)</p>
  
  <p>When running my code I get the exception:</p>
  
  <pre><code>...
  java.lang.reflect.InvocationTargetException
      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 org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class dal.access.DBAccess: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1548)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:300)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.context.annotation.AnnotationConfigApplicationContext.&lt;init&gt;(AnnotationConfigApplicationContext.java:84)
      at dal.test.Application.main(Application.java:20)
      ... 6 more
  Caused by: javax.persistence.PersistenceException: Unable to resolve persistence unit root URL
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:529)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.preparePersistenceUnitInfos(DefaultPersistenceUnitManager.java:402)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.afterPropertiesSet(DefaultPersistenceUnitManager.java:384)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:285)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:317)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1607)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1544)
      ... 17 more
  Caused by: java.io.FileNotFoundException: class path resource [] cannot be resolved to URL because it does not exist
      at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:178)
      at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.determineDefaultPersistenceUnitRootUrl(DefaultPersistenceUnitManager.java:526)
      ... 23 more
  </code></pre>
  
  <p>This exception is similar to an <a href="https://stackoverflow.com/questions/8052535/spring-3-1-without-persistence-xml-gives-unable-to-resolve-persistence-unit-roo">older post</a> here that seemed to be resolved back then, this is why I am not sure it's the same case...</p>
  
  <p>Maybe I'm using an old spring product so, my 'build.gradle' file is as following:</p>
  
  <pre><code>buildscript {
      repositories {
          maven { url "http://repo.spring.io/libs-milestone" }
          mavenLocal()
      }
      dependencies {
          classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6")
      }
  }
  
  apply plugin: 'java'
  apply plugin: 'eclipse'
  apply plugin: 'spring-boot'
  
  sourceSets {
      main {
          java {
              srcDirs = ["src"]
          }
      }
  }
  
  jar {
      baseName = 'dal-jar-test'
      version =  '0.1.0'
  }
  
  repositories {
      mavenCentral()
      maven { url "http://repo.spring.io/libs-milestone" }
      maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
  }
  
  dependencies {
      compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6")
      compile("org.springframework:spring-orm:4.0.0.RC1")
      compile("org.springframework.data:spring-data-jpa:1.4.1.RELEASE")
      compile("org.hibernate:hibernate-entitymanager:4.2.1.Final")
      compile("com.h2database:h2:1.3.172")
      testCompile("junit:junit:4.11")
  }
  
  task wrapper(type: Wrapper) {
      gradleVersion = '1.8'
  }
  </code></pre>
  
  <p>My code is as following:</p>
  
  <p>The <strong>POJO</strong> class:</p>
  
  <pre><code>package dal.entities;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  
  @Entity
  public class EntityA {
  
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private long    id;
      private String  firstName;
      private String  lastName;
  
      protected EntityA() {}
  
      public EntityA(String firstName, String lastName) {
          this.firstName  =   firstName;
          this.lastName   =   lastName;
      }
  
      @Override
      public String toString() {
          return String.format("EntityA[id=%d, firstName='%s', lastName='%s']",
                               id,firstName,lastName);
      }
  }
  </code></pre>
  
  <p>The <strong>Repository</strong> Interface:</p>
  
  <pre><code>package dal.repositories;
  
  import java.util.List;
  
  import org.springframework.data.repository.CrudRepository;
  
  import dal.entities.EntityA;
  
  public interface EntityARepository extends CrudRepository&lt;EntityA, Long&gt; {
      List&lt;EntityA&gt; findByLastName(String lastName);
  }
  </code></pre>
  
  <p>The access layer:</p>
  
  <pre><code>package dal.access;
  
  import javax.sql.DataSource;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
  import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.JpaVendorAdapter;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.Database;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  
  public class DBAccess {
  
      @Bean
      public DataSource dataSource() {
          return new EmbeddedDatabaseBuilder()
                          .setType(EmbeddedDatabaseType.H2).build();
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
          LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
          lef.setDataSource(dataSource);
          lef.setJpaVendorAdapter(jpaVendorAdapter);
          lef.setPackagesToScan("dal.repositories");
  
          return lef;
      }
  
      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
          HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
          hibernateJpaVendorAdapter.setShowSql(false);
          hibernateJpaVendorAdapter.setGenerateDdl(true);
          hibernateJpaVendorAdapter.setDatabase(Database.H2);
  
          return hibernateJpaVendorAdapter;
      }
  
      @Bean
      public PlatformTransactionManager transactionManager() {
          return new JpaTransactionManager();
      }
  }
  </code></pre>
  
  <p>and finally the main class:</p>
  
  <pre><code>package dal.test;
  
  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.context.support.AbstractApplicationContext;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  
  import dal.access.DBAccess;
  import dal.entities.EntityA;
  import dal.repositories.EntityARepository;
  
  @Configuration
  @EnableJpaRepositories
  public class Application {
  
      public static void main(String[] args) {
          AbstractApplicationContext ctx = new AnnotationConfigApplicationContext(DBAccess.class);
          EntityARepository repositoryA = ctx.getBean(EntityARepository.class);
  
          // save a couple entities
          repositoryA.save(new EntityA("boy", "boy"));
          repositoryA.save(new EntityA("girl", "girl"));
  
          // fetch all entities
          Iterable&lt;EntityA&gt; As = repositoryA.findAll();
          for (EntityA entityA : As) {
              System.out.println(entityA);
          }
  
          ctx.close();
      }
  }
  </code></pre>
  
  <p>As you can see by the code each class is at a different package. (maybe this is the problem because when I'm creating all the classes at the same package everything works perfectly) The exception is risen when running the gradle generated jar with the java command-line.</p>
  
  <p>Can someone help me?</p>
  
  <p>Thank you!</p>