Spring mvc + hibernate / jpa - >尽管有@PersistenceContext,但不会注入实体管理器

时间:2016-01-07 19:05:41

标签: java spring hibernate spring-mvc jpa

我有EntityManager的问题。当我尝试在dao类中使用EntityManager时,我得到了空指针异常。因此,尽管有@PersistenceContext注释,但不会注入EntityManager。

我的道:

package com.fido.pia.dao;

import com.fido.pia.model.User;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

@Repository
public class UserDao {

   @PersistenceContext
    protected EntityManager entityManager;

    public User save(User row) {
        if(row.isNew()) {
            entityManager.persist(row);
            return row;
        } else {
            return entityManager.merge(row);
        }
    }
}

Servlet配置:

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

<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        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/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 
    Adds some default beans (HandlerAdapter, HandlerMapping, Binding Initializer...). It also turn on some annotations. 
    Explanation in http://stackoverflow.com/questions/28851306/spring-framework-what-is-the-purpose-of-mvcannotation-driven

    WITHOUT THIS, @RequestMapping ANNOTATIONS ARE LOADED, BUT MAPPING DO NOT WORK!!
    -->
    <mvc:annotation-driven />

<!--    Set loading annotations from classes
    <context:component-scan base-package="com.fido.pia"/>-->

    <!--manual homepage-->
    <mvc:view-controller path="/" view-name="home"/>

    <!--view resolver-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--static resources - request will be handeled by ResourceHttpRequestHandler-->
    <mvc:resources mapping="/resources/**" location="/resources/" />

    <!--database config-->
    <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/pia" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <!--entity manager factory-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="packagesToScan" value="com.fido.pia" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--<property name="generateDdl" value="true" />-->
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

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

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!--Set loading annotations from classes-->
    <context:component-scan base-package="com.fido.pia"/>
</beans>

这里有什么问题吗?

3 个答案:

答案 0 :(得分:0)

在entityManagerFactory bean定义中,试试这个:

<property name="packagesToScan" value="com.fido.pia.*" />

这应该有效

答案 1 :(得分:0)

我终于解决了。问题是我使用常见的依赖注入来在控制器中注入我的dao类。当我使用自动装配将其更改为DI(将@autowired添加到控制器构造函数)时,dao中的实体管理器被初始化。

所以现在它有效,但我仍然很好奇为什么这种变化如此重要。我问过new question about it

答案 2 :(得分:0)

您需要启用持久性注释:

<context:annotation-config/>

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