Spring mvc + hibernate / jpa - >尽管@PersistenceContext不会注入实体管理器 [英] Spring mvc + hibernate/jpa -> entity manager is not injected despite @PersistenceContext

查看:105
本文介绍了Spring mvc + hibernate / jpa - >尽管@PersistenceContext不会注入实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EntityManager的问题。当我尝试在dao类中使用EntityManager时,我得到了空指针异常。因此,尽管使用了@PersistenceContext注解,EntityManager仍未被注入。



我的dao:

  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
公共类UserDao {

@PersistenceContext
受保护的EntityManager entityManager;
$ b $ public用户保存(用户行){
if(row.isNew()){
entityManager.persist(row);
返回行;
} else {
return entityManager.merge(row);
}
}
}

Servlet配置:

 <?xml version =1.0encoding =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\"> ;

<! - -
添加一些默认的Bean(HandlerAdapter,HandlerMapping,Binding Initializer ...)。它也打开了一些注释。
解释在http://stackoverflow.com/questions/28851306/spring-framework-what-is-the-purpose-of-mvcannotation-driven

没有这个,@RequestMapping注解加载,但映射不工作!
- >
< mvc:annotation-driven />

<! - 设置从类加载注释
< context:component-scan base-package =com.fido.pia/> - >

<! - 手册首页 - >
< mvc:view-controller path =/view-name =home/>

<! - 视图解析器 - >
< bean class =org.springframework.web.servlet.view.InternalResourceViewResolver>
< property name =prefixvalue =/ WEB-INF / view //>
< property name =suffixvalue =。jsp/>
< / bean>

<! - 静态资源 - 请求将由ResourceHttpRequestHandler处理 - >
< mvc:resources mapping =/ resources / **location =/ resources //>

<! - database config - >
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / pia/>
< property name =usernamevalue =root/>
< property name =passwordvalue =/>
< / bean>

<! - - 实体经理工厂 - >
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =packagesToScanvalue =com.fido.pia/>
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
<! - < property name =generateDdlvalue =true/> - >
< property name =showSqlvalue =true/>
< / bean>
< / property>
< / bean>

<! - 交易 - >
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>

<! - 启用基于注释的事务行为配置 - >
< tx:注解驱动的事务管理器=transactionManager/>

<! - 设置从类加载注释 - >
< context:component-scan base-package =com.fido.pia/>
< / beans>

任何想法在这里有什么不对?

解决方案

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

所以现在它可以工作,但我仍然很好奇关于为什么变化如此重要。我已经问过关于它的新问题


I have a problem with EntityManager. When I try to use EntityManager in a dao class, I got null pointer exception. So EntityManager is not injected despite @PersistenceContext annotation.

My dao:

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 Config:

<?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>

Any ideas what is wrong here?

解决方案

I finally solve it. The problem was that I use common dependency injection to inject my dao class in controller. When I change it to DI with autowired (added @autowired to controller constructor), entity manager in dao is initialized.

So now it works, but I'm still curious about why is that change so important. I've asked new question about it.

这篇关于Spring mvc + hibernate / jpa - &gt;尽管@PersistenceContext不会注入实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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