【问题标题】:JPA Spring injection EntityManager is null even if persistence unit entity is started即使启动了持久性单元实体,JPA Spring 注入 EntityManager 也为空
【发布时间】:2014-05-29 12:20:18
【问题描述】:

我正在使用 JPA 和 spring 将我的 JBOss 服务器连接到 oracle 数据库。

这是我的配置:

数据库.xml

<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
    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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/task 
        http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config />

    <context:component-scan base-package="com.example" />

    <!-- Add JPA support -->
    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="unitDS" />
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
        <property name="loadTimeWeaver">
            <bean
                class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
        </property>
    </bean>

    <!-- Add Transaction support -->
    <bean id="mtsTxManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
    </bean>

    <!-- Use @Transaction annotations for managing transactions -->
    <tx:annotation-driven transaction-manager="mtsTxManager"
        proxy-target-class="true" />

</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- @version $Date: 2010-06-04 15:50:29 +0200 (Fri, 04 Jun 2010) $ | $LastChangedBy: 
    ext_computaris_eprager $ | LastChangedRevision: $Rev: 2424 $ -->
<persistence version="2.0"
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="unitDS"
        transaction-type="RESOURCE_LOCAL">

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:jboss/datasources/unitDS</non-jta-data-source>


        <class>com.example.foo</class>
        <class>com.example.bar</class>



        <!-- properties specific for the underlying implementation of the JPA EntityManager 
            (Hibernate) -->
        <properties>
            <!-- ////////////// HBM/DDL related properties ////////////// were removed 
                from here (set as system properties in tests) -->
            <!-- NCA -->
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="jboss.entity.manager.factory.jndi.name"
                value="java:/locationserverEntityManagerFactory" />

            <!-- ////////////// Enable JTA transaction ////////////// -->
            <property name="transaction.factory_class"
                value="org.hibernate.transaction.JTATransactionFactory" />
            <property name="jta.UserTransaction" value="java:comp/UserTransaction" />
            <property name="hibernate.transaction.manager_lookup_class"
                value="org.hibernate.transaction.JBossTransactionManagerLookup" />
            <!-- ////////////// debugging/testing related properties ////////////// -->
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />

            <!-- 2nd level cache -->
<!--            <property name="hibernate.cache.provider_class" -->
<!--                value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" /> -->
<!--            <property name="net.sf.ehcache.configurationResourceName" -->
<!--                value="/ehcache.xml" /> -->
<!--            <property name="hibernate.cache.use_query_cache" value="true" /> -->
<!--            <property name="hibernate.cache.use_second_level_cache" -->
<!--                value="true" /> -->
<!--            <property name="hibernate.generate_statistics" value="true" /> -->
        </properties>
    </persistence-unit>

</persistence>

spring 上下文正常初始化。我收到了消息:

Starting persistence unit unitDS

当我启动我的服务器时。

我还有一个抽象的 dao JPA,我在其中使用注解 @persistenceContext 注入我的上下文:

public class AbstractDaoJpa<E, PK> implements DaoBase<E, PK> {

    /**
     * 
     */
    @PersistenceContext(unitName="unitDS")
    @Qualifier(value = "entityManagerFactory")
    private EntityManager em;

问题是当我尝试连接到我的数据库时,我的 EntityManager 始终为空。

我不明白这个问题来自哪里。我有错误的配置吗?

我尝试了几个类似帖子的解决方案,但无法解决我的问题。

【问题讨论】:

  • 如果删除限定符和/或 unitName 会发生什么?
  • 试图删除它,没有改变。当我保留@persistenceContext 时设置上下文,否则不设置(这是正常的)。

标签: java spring hibernate jpa


【解决方案1】:

我不确定,如果这是问题所在,但在您的 Spring 配置中,您将 emf 作为 bean 名称,而在 AbstractDao 中,您将 entityManagerFactory 作为限定符。所以我猜 Spring 没有注入正确的 bean。

【讨论】:

  • 抱歉没有解决问题,我从上面创建的 LocalContainerEntityManagerFactoryBean 中引用了 emf 以获得事务支持。问题不在于这个
【解决方案2】:

您似乎没有添加启用组件扫描的&lt;context:component-scan base-package="your.package" /&gt;。你为什么不用合适的包试试呢

【讨论】:

  • 我添加了但没有解决问题(对不起应该说我之前尝试过这个解决方案)
  • @BoyBoy 如果我想到其他任何事情,我会回复你
【解决方案3】:

我知道已经晚了,但我会写下来以备将来参考。使用 new 关键字创建实现实例时有一个问题。有关更多详细信息,您可以查看this thread

【讨论】:

    【解决方案4】:

    它可能有某些因素 * persistence.xml 中的持久性提供程序缺失

    • 使用任何配置设置 applicationContext.xml 后,您将获得 entitymanagerfactory,即来自 LocalEntity、LocalContainer 或 JNDI Lookup。

      使用持久化上下文

        private EntityManager em;
      

    用 @transactional ,@Repository 注释的类

    但是你仍然得到 em = null 背后的原因是你没有使用依赖注入来调用你的类。

    【讨论】:

      猜你喜欢
      • 2015-05-03
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      • 2021-05-06
      相关资源
      最近更新 更多