当前位置: 首页 > 知识库问答 >
问题:

创建JpaRepostory时出错

长孙景焕
2023-03-14

我有一个带有Spring Data JPA的Web应用程序。我为一些实体创建了扩展JpaRepository的存储库。但是,当我尝试部署应用程序时,会遇到以下错误。不知道为什么会抛出错误

应用程序错误日志:

ERROR org.springframework.web.context.ContextLoader  - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryTest' defined in file [C:\Tomcat\apache-tomcat-7.0.53-internal\webapps\appTest\WEB-INF\classes\applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
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:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
.....
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException
at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1456)
at org.hibernate.cfg.annotations.CollectionBinder.bindOneToManySecondPass(CollectionBinder.java:864)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:779)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:728)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:150)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 25 more

pom.xml

<properties>
    <spring.framework.version>4.0.6.RELEASE</spring.framework.version>
    <spring-data-jpa>1.6.1.RELEASE</spring-data-jpa>

    <hibernate-core>4.3.4.Final</hibernate-core>
    <hibernate-validator>5.1.0.Final</hibernate-validator>
    <hibernate-entitymanager>4.3.4.Final</hibernate-entitymanager>
</properties>

....

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-oxm</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa}</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>${jtds}</version>
    </dependency>

    <!-- hibernate -->  
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate-core}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate-validator}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate-entitymanager}</version>
    </dependency>
...     

应用程序上下文.xml

<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
xmlns:task="http://www.springframework.org/schema/task" 
xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<context:component-scan base-package="uk.ac.universityName" />

<jpa:repositories base-package="uk.ac.universityName.dao.etocbrs"
    entity-manager-factory-ref="entityManagerFactoryTest"
    transaction-manager-ref="transactionManagerEtocBrs" />

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

<!-- ETOC_BRS JPA Entity Manager Factory -->
<bean id="entityManagerFactoryTest" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnitEtocBrs"/> 
    <property name="dataSource" ref="dataSourceEtocBrs"/>
    <property name="persistenceProviderClass" value="org.hibernate.jpa.HibernatePersistenceProvider"/>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>               
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

......

persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence 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"
version="2.0">

<persistence-unit name="persistenceUnitEtocBrs" />

<persistence-unit name="persistenceUnitEtocAdmin" />

</persistence>

JpaRepostory接口

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import org.springframework.data.jpa.repository.JpaRepository;

import uk.ac.universityName.model.etocbrs.Item;

public interface ItemRepository extends JpaRepository<Item, Long> {

    @Query("SELECT e FROM Item e")
    List<Item> findUnprocessed(Pageable pageable);
}

共有2个答案

罗光华
2023-03-14

实体类中有一些错误,我已经通过它们之间的正确映射来修复它。

党权
2023-03-14

这是我的建议,好像spring不能实例化entitymanager,所以请尽量使用你的persistence.xml如下。

<persistence-unit name="persistenceUnitEtocBrs" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>where.your.item.class.is.Item</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
        <property name="hibernate.hbm2ddl.auto" value="validate" />
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        <property name="hibernate.connection.charSet" value="UTF-8" />
        <property name="hibernate.show_sql" value="false" />
    </properties>
</persistence-unit>

我也不确定你是否需要为spring4.0做一些不同的事情。

<spring.data.version>1.3.1.RELEASE</spring.data.version>
<spring.version>3.2.5.RELEASE</spring.version>
<hibernate.version>4.2.0.Final</hibernate.version>
 类似资料:
  • 我试图编译一个非常简单的程序,将包含3个用户的简单表保存到http://localhost/phpmyadmin,以清空名为,users ' '的数据库,但它仍然显示异常,您可以看到。 1个异常org.springframework.beans.factory。BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure

  • 当我启动Weblogic时(使用jar:hibernate-core-4.3.6.final.jar和hibernate-jpa-2.1-api-1.0.0.final.jar),遇到以下错误信息: 无法自动连接字段:private org.hibernate.sessionFactory com.nscorp.lars.shopleveling.core.dao.impl.Dataloaddao

  • 我在SpringBoot应用程序中创建HighHendRestClient bean时遇到一个错误。我已经做了一个测试'app',在那里我检查了我可以实例化我想要的对象,然后进行我想要的调用,我现在正在做一个新的应用程序的婴儿步骤。 就我所能看到的(我还没有用它做太多...) 当我添加它时(最初我传入了RestClient bean,但现在我临时创建了一个本地对象,以便更清晰) 我得到这个java

  • 我使用的是Spring 3.1.4 服务实现 DAO实现 web.xml

  • 现在我正在尝试用spring MVC配置jpa数据。但是我在tomcat服务器上尝试执行时遇到了异常。 有人能指导我解决这个吗?谢谢

  • 严重:上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建ServletContext资源[/web-inf/mvc-dispatcher-servlet.xml]中定义的名为“org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerM