Spring Data JPA:类路径资源[]无法解析为URL,因为它不存在

时间:2013-12-22 22:56:18

标签: spring hibernate jpa-2.0 spring-data-jpa

我是Spring的新手,但我决定将Spring-Data-JPA用于个人项目。 我开始关注"Accessing Data with JPA"教程,但我自己修改了。 (如下所示)

运行我的代码时,我得到了异常:

...
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.<init>(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

这个例外类似于older post这里似乎已经解决了,这就是为什么我不确定它是同样的情况......

也许我正在使用旧的弹簧产品,所以我的'build.gradle'文件如下:

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'
}

我的代码如下:

POJO 类:

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);
    }
}

存储库界面:

package dal.repositories;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

import dal.entities.EntityA;

public interface EntityARepository extends CrudRepository<EntityA, Long> {
    List<EntityA> findByLastName(String lastName);
}

访问层:

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();
    }
}

最后是主要课程:

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<EntityA> As = repositoryA.findAll();
        for (EntityA entityA : As) {
            System.out.println(entityA);
        }

        ctx.close();
    }
}

正如您可以通过代码看到的,每个类都在不同的包中。 (也许这就是问题所在,因为当我在同一个包中创建所有类时,一切都运行正常)当使用java命令行运行gradle生成的jar时会出现异常。

有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

基于您提供的示例和Spring数据示例项目:

如果将实体放入不同的包中,则应明确指定spring应该查找这些实体的包,它不能真正扫描所有项目,因为从性能角度和内存消耗两者都不合理

因此,在您的示例中,实体存在于“dal.entities”包中。 您应该像这样配置LocalContainerEntityManagerFactoryBean:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
    LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
    lef.setDataSource(dataSource);
    lef.setJpaVendorAdapter(jpaVendorAdapter);
    lef.setPackagesToScan("dal.entities"); // !!! you might want to place here more than one package, its vararg
    return lef;
}

我已经下载了示例项目(我没有gradle,所以我使用了maven build),将实体放在不同的包中并且工作正常。)

如果你不想配置你的东西,你可能要考虑创建一个持久性xml,它将指定这些实体的列表,但它是一个完全不同的故事。

希望这有帮助