无法在项目SpringData

时间:2019-01-19 21:22:53

标签: java spring-data-jpa

我在Maven中执行spring数据项目jpa,出现了以下错误,我不知道为什么。这是我的POM.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>SpringData</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
    </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-commons</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <name>SpringData</name>
</project>

这是一个使用Spring Data Jpa的项目。

我不知道失败可能是什么

错误提示:

  

SLF4J:无法加载类“ org.slf4j.impl.StaticLoggerBinder”。   SLF4J:默认为无操作(NOP)记录器实现SLF4J:   有关更多信息,请参见http://www.slf4j.org/codes.html#StaticLoggerBinder   细节。烯20,2019 11:21:09 AM   org.hibernate.jpa.internal.util.LogHelper   logPersistenceUnitInformation信息:HHH000204:处理中   PersistenceUnitInfo [name:default ...] ene 20,2019 11:21:09 AM   org.hibernate.Version logVersion信息:HHH000412:Hibernate Core   {5.2.8.Final} ene 20,2019 11:21:09 org.hibernate.cfg.Environment    信息:HHH000206:2019年20月20日未找到hibernate.properties   上午11:21:09   org.hibernate.annotations.common.reflection.java.JavaReflectionManager    INFO:HCANN000001:休眠公共注释   {5.0.1.Final} ene 20,2019 11:21:09 org.hibernate.dialect.Dialect    信息:HHH000400:使用方言:   org.hibernate.dialect.MySQLDialect ene 20,2019 11:21:10上午   org.hibernate.hql.internal.QueryTranslatorFactoryInitiator   initialService信息:HHH000397:使用ASTQueryTranslatorFactory   线程“主”中的异常   org.springframework.beans.factory.BeanCreationException:错误   创建名称为“ clienteRepositorio”的bean:init的调用   方法失败;嵌套的异常是java.lang.IllegalArgumentException:   EntityPathResolver不能为null!在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1745)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)     在   org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:320)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)     在   org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:827)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:144)     在   org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:85)     在principal.Principal.main(Principal.java:12)的原因:   java.lang.IllegalArgumentException:EntityPathResolver不能为   空值!在org.springframework.util.Assert.notNull(Assert.java:198)处   org.springframework.data.jpa.repository.support.JpaRepositoryFactory.setEntityPathResolver(JpaRepositoryFactory.java:111)     在   org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.createRepositoryFactory(JpaRepositoryFactoryBean.java:105)     在   org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.doCreateRepositoryFactory(JpaRepositoryFactoryBean.java:96)     在   org.springframework.data.repository.core.support.TransactionalRepositoryFactoryBeanSupport.createRepositoryFactory(TransactionalRepositoryFactoryBeanSupport.java:80)     在   org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:270)     在   org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:119)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)     ...另外12个   -------------------------------------------------- ----------------------建立失败   -------------------------------------------------- ----------------------总时间:4.017秒完成时间:2019年1月20日周日CET决赛   内存:6M / 245M   -------------------------------------------------- ----------------------无法执行目标org.codehaus.mojo:exec-maven-plugin:1.2.1:exec   (default-cli)在项目SpringData上:命令执行失败。处理   退出并出现错误:1(退出值:1)-> [帮助1]

     

要查看错误的完整堆栈跟踪,请使用-e重新运行Maven。   开关。使用-X开关重新运行Maven以启用完整的调试日志记录。

     

有关错误和可能的解决方案的更多信息,请   阅读以下文章:[帮助1]   http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

和ApplicationContext:

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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.1.xsd
            http://www.springframework.org/schema/data/jpa
            http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <jpa:repositories base-package="dao"/>

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="dominio" />
            <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL"/>       
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/BClientes" />
            <property name="username" value="root" />
            <property name="password" value="" />
    </bean>

</beans>

和Principal.java

package principal;

import dao.ClienteRepositorio;
import dominio.Cliente;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Principal {

    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ClienteRepositorio clientes = (ClienteRepositorio) context.getBean("clienteRepositorio");

        Cliente cliente;

        Cliente cliente1 = new Cliente(1, "pwjuan", "Juan Alcaraz");
        Cliente cliente2 = new Cliente(2, "pwpedro", "Pedro Zamora");
        Cliente cliente3 = new Cliente(3, "pwluis", "Luis Valera");

        // Borramos todo, para no tener que ejecutar el script de la base cada vez
        for (Cliente c : clientes.findAll())
            clientes.delete(c);

        System.out.println();
        System.out.println("Alta de clientes en proceso");
        clientes.save(cliente1);
        clientes.save(cliente2);
        clientes.save(cliente3);

      }


}

项目结构:https://gyazo.com/37d13670db39aa5c887ddb72f62574ef

2 个答案:

答案 0 :(得分:0)

您的应用程序上下文缺少JPA架构参考。

此行为您提供问题的位置:

  

来自类路径资源的XML文档中的第14行   [applicationContext.xml]无效;嵌套异常为   org.xml.sax.SAXParseException; lineNumber:14; columnNumber:43;艾尔   prefijo“ jpa” para el elemento“ jpa:repositories” noestáenlazado。

解决方案是正确引用jpa模式:

<beans
    xmlns="http://www.springframework.org/schema/beans"
    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.1.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
            ...
            >

请注意您的context.xml缺少xmlns:jpa部分。

答案 1 :(得分:0)

尝试使用Maven Standard Directory Layout整理文件:

src/
  main/
    java/
      (put your .java files here)
    resources/
      applicationContext.xml

查看已编译的源文件(target文件夹或.jar文件-请注意,您可以使用与ZIP兼容的存档器(例如7-ZIP)打开后者)并验证您的{{1} }确实在更改文件夹结构之前丢失,并且在应用文件夹并编译项目后仍然存在。

此问题的读者的更新:作者已多次更改该问题。修复前一个错误后,每次报告一个全新的错误。这个答案是整个过程的第一步,它使系统可以在运行时实际找到applicationContext.xml