簡體   English   中英

休眠和春季:EntityManager為空

[英]Hibernate & Spring: EntityManager is Null

我正在用Spring和Hibernate建立一個Project。 我創建了一個包含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();
}

}

提前致謝!

實體管理器只能注入事務類中。 換句話說,它只能注入到EJB中。 其他類必須使用EntityManagerFactory來創建和銷毀EntityManager。

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

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

EntityManager entityManager = entityManagerFactory.createEntityManager();

然后使用emtityManager執行操作。

或者,您可以通過JNDI查找使用容器管理的事務。

讓我知道任何問題。

嘗試將<context:annotation-config />到您的持久性上下文中。 參考手冊的節可能幫助您使用@PersistenceContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM