Hibernate& Spring:EntityManager是Null

时间:2014-04-26 15:40:07

标签: java spring hibernate

我正在使用Spring和Hibernate设置项目。我创建了一个包含EntityManager的抽象DAO类。当我想使用这个EntityManager时,它是null,我不明白为什么。也许你可以帮助我。

我的持久性背景

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/mvc        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
   http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/tx         http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<import resource="persistence-beans.xml" />
<mvc:annotation-driven />

<bean id="jdbcPropertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="classpath:META-INF/mysql.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}"
    p:username="${jdbc.username}" p:password="${jdbc.password}" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="${hibernate.show_sql}" />
            <property name="generateDdl" value="${hibernate.generate_ddl}" />
            <property name="databasePlatform" value="${hibernate.dialect}" />
        </bean>
    </property>
</bean>

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

PersonDao.java:

@Repository
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PersonDao extends AbstractDao<Person> {

    public PersonDao() {
    super(Person.class);
}
}

AbstractDao.java

@SuppressWarnings("unchecked")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public abstract class AbstractDao<T> {

@PersistenceContext
protected EntityManager em;

private Class<T> entityClass;

public AbstractDao(Class<T> entityClass) {
    this.entityClass = entityClass;
}

public AbstractDao() {
}

public void create(T entity) {
    this.em.persist(entity);
}

public void edit(T entity) {
    this.em.merge(entity);
}

public void remove(T entity) {
    this.em.remove(this.em.merge(entity));
}

public T find(long primaryKey) {
    return this.em.find(entityClass, primaryKey);
}

@SuppressWarnings("rawtypes")
public List<T> findAll() {
    CriteriaQuery cq = this.em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    return this.em.createQuery(cq).getResultList();
}

}

提前致谢!

2 个答案:

答案 0 :(得分:1)

实体管理器只能注入事务类。换句话说,它只能注入EJB。其他classe必须使用EntityManagerFactory来创建和销毁EntityManager。

但您的AbstractDAO不是EJB,您不能直接使用EntityManager。

溶液

@PersistenceUnit(unitName = "test")
private EntityManagerFactory entityManagerFactory;

EntityManager entityManager = entityManagerFactory.createEntityManager();

然后使用emtityManager执行操作。

或者,您可以使用JNDI查找来使用容器管理的事务。

让我知道任何问题。

答案 1 :(得分:0)

尝试将<context:annotation-config />添加到持久性上下文中。参考手册的This部分可以帮助您使用@PersistenceContext