空指针异常实体管理器 [英] Null Pointer Exception Entity Manager

查看:100
本文介绍了空指针异常实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试坚持和帐户时,我得到了一个空指针异常.原来entityManager为空,但我不知道为什么.这是相关的代码,有什么想法吗?我已经尝试过

I am getting a Null Pointer exception when i try to persist and Account. Turns out the entityManager is null but I can't figure out why. Here is the relevant code, any ideas? I have tried to do

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>

推荐答案

将以下内容添加到您的Spring配置中.

Add the following to your Spring config.

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

这篇关于空指针异常实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆