空指针异常实体管理器

时间:2013-04-25 22:56:25

标签: java spring jpa eclipselink

当我尝试保持和帐户时,我得到一个空指针异常。结果entityManager为空,但我无法弄清楚原因。这是相关的代码,任何想法?我试过了

package com.test.bank2.account;

import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Transactional
public class AccountRepositoryImpl implements AccountRepository{
    @PersistenceContext(unitName = "entityManagerFactory")
    private EntityManager entityManager;


    @Override
    public void save(Account account) {

        entityManager.persist(account);
    }
}

和配置:

<?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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


    <jdbc:embedded-database id="dataSource">
        <jdbc:script location="classpath:setup.sql"/>
    </jdbc:embedded-database>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.test.bank2" />
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.weaving" value="false"/>
                <entry key="eclipselink.ddl-generation" value="create-tables"/>
                <entry key="eclipselink.target-database" value="org.eclipse.persistence.platform.database.HSQLPlatform"/>
            </map>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter" >
                <property name="databasePlatform" value="org.eclipse.persistence.platform.database.HSQLPlatform" />
                <property name="generateDdl" value="true" />
            </bean>
        </property>        
    </bean>

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


    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

2 个答案:

答案 0 :(得分:3)

将以下内容添加到Spring配置中。

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

答案 1 :(得分:0)

您可能希望在尝试在任何地方使用它之前尝试将您的entityManager设置为'new EntityManager()',可能是在您声明它或声明它之后立即使用它。