【问题标题】:Null EntityManager Spring 4空 EntityManager Spring 4
【发布时间】:2014-07-24 01:37:22
【问题描述】:

我正在尝试使用 Spring 4 设置实体管理器,当我尝试使用 @PersistenceContext 注释注入 EntityManager 时,我总是得到 NullPointerException

我有 Maven 网络应用程序。这是我的 applicationContext.xml 配置

<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="EcommercePU">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
        <list>
            <value>com.mysite.ecommerceapp.domain</value>
            <value>com.mysite.ecommerceapp.domain.*</value>
            <value>com.mysite.ecommerceapp.domain.*.*</value>
            <value>com.mysite.ecommerceapp.domain.*.*.*</value>
        </list>
    </property> 
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/ecommercedb" />
    <property name="username" value="postgres" />
    <property name="password" value="test" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="myEmf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="persistenceExceptionTranslationPostProcessor"
     class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

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

applicationContext.xml 位于 WEB-INF 文件夹下。因为我使用 NetBeans 8,所以我从项目属性 -> 框架中为我的项目添加了 Spring Framework 支持。我正在使用 EntityManager 注入和名为 GenericDaoImpl 的文件,这是它的代码:

public abstract class GenericDaoImpl<E extends EntityClass> implements GenericDao<E> {

private Class<E> entityClass;

@PersistenceContext()
private EntityManager entityManager;

//Constructor
public GenericDaoImpl(final Class<E> entityClass) {
    this.entityClass = entityClass;
}

//Getters and Setters
public Class<E> getEntityClass() {
    return entityClass;
}

public void setEntityClass(final Class<E> entityClass) {
    this.entityClass = entityClass;
}

public EntityManager getEntityManager() {
    return entityManager;
}

public void setEntityManager(final EntityManager entityManager) {
    this.entityManager = entityManager;
}

public void checkEntityManager() {
    System.out.println("em: " + this.entityManager);
}
}

我有一个测试文件,我只需使用 checkEntityManager() 方法打印实体管理器。每当我运行它时,我总是得到 EntityManager 的 null 值。有什么问题?我最大的疑问是我的 applicationContext.xml 放错了地方,或者它没有在任何地方使用,因此在 applicationContext 中找不到配置属性。我已经测试过将 applicationContext.xml 文件移动到资源文件夹,但它也没有帮助。我还有其他问题如下:

  1. 我需要有 persistence.xml 吗?由于我使用带有 packagesToScan 功能的 Spring 4,我听说persistence.xml 不是必需的,但我不确定。

  2. 使用 hibernate 只处理基本实体 crud 和其他带有 JdbcTemplate 的 dao 查询是否明智?

我们将不胜感激。

【问题讨论】:

  • 如何创建扩展GenericDaoImpl 的子类的实例(由new 创建,让spring 注入它们(@Autowired)或每个context.xml)?
  • 嗨拉尔夫。我用new 关键字实例化子类。但我根本没有使用过@Autowired。除了以下行 &lt;Context antiJARLocking="true" path="/ECommerceApp"/&gt; 之外,我在 context.xml 文件中没有其他任何内容
  • 如果使用new创建实例,Spring无从知晓,无法注入实体管理器。您必须在另一个 Spring bean 中自动装配它们。
  • 嗨,JB,但为了自动装配,我不应该在 context.xml 文件中添加所有可自动装配的类吗?另一件事是,当我有一个扩展上面的 GenericDaoImpl 的 dao 类时,我用 new 关键字实例化它并测试 EntityManager 它仍然给我一个 NullPointerException

标签: java spring hibernate web-applications entitymanager


【解决方案1】:

我终于解决了这个问题,还有很多事情要做。

  1. 当我通过 JUnit 测试我的 Entity Manager 时,我得到了NullPointerException,原因是配置文件的文件夹不正确。

  2. applicationContext.xml 已正确设置,但有一个小问题导致整个文件混乱,那就是 &lt;property name="persistenceUnitName" value="EcommercePU"&gt;。因为我在整个项目中没有这样的持久性单元文件,所以找不到它。当您添加该属性时,这并不意味着您已经创建了具有此类持久性单元名称的 spring 配置文件。它只是意味着如果您想为您的LocalContainerEntityManagerFactory 提供持久性配置,您将使用该名称来引用您的持久性单元。这在 Spring 4 中不是必需的。

  3. applicationContext.xml 必须从 WEB-INF 移动到资源文件夹。

  4. 当您运行 JUnit 测试时,您应该使用 @Autowired 注释来注释 DAO。

  5. Dao 实现类应该有@Repository 注解。

  6. @Transactional 如果要使用,至少对于 CRUD 方法是必需的。您还可以注释整个班级。如果不添加@Transactional 注解EntityManager 将无法正确执行persist、remove、update、find 等方法。如果您使用 Spring 配置,请记住这是必需的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-02
    • 2014-05-16
    • 2016-08-06
    • 1970-01-01
    • 2012-12-01
    • 2023-04-10
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多