gpt4 book ai didi

java - hibernate 和 Spring : EntityManager is Null

转载 作者:行者123 更新时间:2023-12-01 13:05:15 24 4
gpt4 key购买 nike

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

我的持久性上下文

<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();
}

}

提前致谢!

最佳答案

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

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

解决方案

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

EntityManager entityManager = entityManagerFactory.createEntityManager();

然后使用emtityManager执行操作。

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

如有任何问题请告诉我。

关于java - hibernate 和 Spring : EntityManager is Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com