我遇到了将实体管理器与应用程序上下文中存在的bean连接起来的问题。 每当我做一些操作时,它都会产生NullPointerException。
这是我的applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.ajit.retail"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost/retail"/>
<property name="username" value="retail_user"/>
<property name="password" value="password"/>
</bean>
<bean id="entityManagerOne" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
</bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerOne"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
这是我正在创建实体管理器的java文件
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
public class AbstractRepository {
@PersistenceContext
EntityManager entityManager;
}
所以每当我使用这个实体管理器时,它都会给出空指针异常 请帮忙!
答案 0 :(得分:0)
您的实体管理器bean称为enetityManagerOne,但该变量称为entityManager。也许在你的XML文件中重命名你的bean:
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
另一种解决方案可能是您忘记了以下bean声明:
支持交易:
<tx:annotation-driven/>
支持解析JPA注释:
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
答案 1 :(得分:0)
我的应用程序上下文不在正确的位置(src / main / resources)。现在我把它放在那里,它的工作。
答案 2 :(得分:0)
在dispatcher-servlet.xml中使用此代码
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
<list>
<value>classpath*:META-INF/persistence.xml</value>
</list>
</property>
<property name="defaultDataSource" ref="dataSource"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitManager" ref="persistenceUnitManager"/>
<property name="persistenceUnitName" value="etray"/>
</bean>
<jee:jndi-lookup id="dataSource" jndi-name="java:/prateek" />
<!-- we plan to use JTA transactions and declare them using annotations -->
<bean id="transactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- to inject instances of EntityManager using @PersistenceContext annotation -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
它解决了我的问题,希望它会有所帮助。