无法在持久层类中注入EntityManager

时间:2015-08-06 09:19:51

标签: java spring hibernate jpa

使用Hibernate 4.1 - Spring 4 - Jpa 2 - Jersey

我几天后就遇到了问题。 每当我尝试在我的类中注入一个EntityManager时,它最终会出现空指针异常。 以下是我称之为实体经理的课程

@PersistenceContext(unitName = "Student")
private EntityManager em;
/**
 * This method returns all the items
 * @return List of all items
 */
@Transactional
public List<Item> getAllItems() {
    //EntityManager em =  Persistence.createEntityManagerFactory("Student").createEntityManager();
    Query query = em.createNamedQuery("Item.getAllItems");
    List<Item> list = new ArrayList<>();
    try{
         list =  query.getResultList();
    }finally{
       em.close(); 
    }
    return list;
}

这是我的ApplicationContext.xml文件:

 ==============================================================================================================
                                               DATASOURCE
      ==============================================================================================================   -->
 <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/dbname?zeroDateTimeBehavior=convertToNull" />
    <property name="username" value="username" />
    <property name="password" value="pwd" />
</bean>

<!-- ==============================================================================================================
                                               ENTITY MANAGER FACTORY
      ==============================================================================================================   -->
 <bean id="emf"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="Student" />
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="database" value="MYSQL" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="eclipselink.weaving">static</prop>
            <prop key="eclipselink.ddl-generation">create-tables</prop>
            <prop key="eclipselink.ddl-generation.output-mode">database</prop>
        </props>
    </property>
</bean>
<!-- ==============================================================================================================
                                               TRANSACTION MANAGER
      ==============================================================================================================   -->
 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>

现在,我正在为我的每个方法创建一个实体管理器,这可能不是处理它的最佳方法。

提前致谢

编辑1

新配置

<context:annotation-config />
<context:component-scan base-package="com.mypackage.service" /> 
<context:component-scan base-package="com.mypackage.repository" />

                                            

<bean id="emf"
       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="Student" />
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter">
        <bean
            class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
            <property name="database" value="MYSQL" />
        </bean>
    </property>
    <property name="jpaProperties">
        <props>
            <prop key="eclipselink.weaving">static</prop>
            <prop key="eclipselink.ddl-generation">create-tables</prop>
            <prop key="eclipselink.ddl-generation.output-mode">database</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>


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

1 个答案:

答案 0 :(得分:0)

正如@Reimeus所说,你需要配置Spring以关注存储库类。

一种方法是告诉Spring哪个和哪个注释类应该扫描。有很多教程示例说明了如何执行此操作(例如,检查this tutorial Spring配置部分中的context:component-scan标记)。接下来,我将向您展示一个要在ApplicationContext文件中添加的模板

<context:component-scan base-package="<full_package_name_where_is_your_Repository_class>">
    <context:include-filter type="annotation"
        expression="org.springframework.stereotype.Repository" />
</context:component-scan>

要使其正常工作,请设置正确的base-package值,同时添加名称空间xmlns:context="http://www.springframework.org/schema/context"和架构位置http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd