java - EntityManager 始终为 null 并且未正确注入(inject)

标签 java spring hibernate annotations

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.test.dao.CustomerDaoImpl</class>
    <class>com.test.data.Customer</class>
    <class>com.test.dto.CustomerDto</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.hbm2ddl.auto" value="update"/>
        <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy"/>
        <property name="hibernate.connection.charSet" value="UTF-8"/>
    </properties>
</persistence-unit>

我使用它的类:

public class CustomerDaoImpl implements CustomerDao {

@PersistenceContext(unitName = "persistenceUnit")
private  EntityManager entityManager;

@Transactional
public List<CustomerDto> getCustomers() {
    List<CustomerDto> customers = null;
    List<Customer> cust = new ArrayList<Customer>();
    Query q = entityManager.createQuery(
            "SELECT c"
            + " FROM Customer c ");

编辑添加 SPRING-SERVLET.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">

    <context:spring-configured />
    <context:component-scan base-package="com.test"/>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/testdb" />
        <property name="username" value="root" />
        <property name="password" value="password" />
        <property name="testOnBorrow" value="true" />
        <property name="testOnReturn" value="true" />
        <property name="testWhileIdle" value="true" />
        <property name="timeBetweenEvictionRunsMillis" value="1800000" />
        <property name="numTestsPerEvictionRun" value="3" />
        <property name="minEvictableIdleTimeMillis" value="1800000" />
    </bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
    id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven mode="aspectj"
    transaction-manager="transactionManager" />

<bean
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit" />
    <property name="dataSource" ref="dataSource" />
</bean>

<bean
    class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean
    class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/js/" />
    <property name="suffix" value=".js" />
</bean>

测试后,我发现 Netbeans 在 CustomerDaoImpl 类中给了我错误,它说:“类在 persistence.xml 中列出,但没有注释”。在这种情况下,正确的注释是什么?

我可以从日志中看到pu有正确的entitymanager:Closing JPA EntityManagerFactory for persistence unit 'persistenceUnit'并且我正在创建这样的dao:

@Autowired
CustomerDao customerDao;

使用@autowired注解时,customerDao为null,所以我尝试使用创建dao

customerDao = new CustomerDaoImpl();

然后entitymanager为NULL。

最佳答案

persistence.xml 中您不需要

<class>com.test.dao.CustomerDaoImpl</class>
<class>com.test.dto.CustomerDto</class>

仅使用

<class>com.test.data.Customer</class>

关于java - EntityManager 始终为 null 并且未正确注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34532108/

相关文章:

java - Spring Boot - 可以拦截@Async 服务方法吗?

java - 如何根据 Windows AD 对用户进行身份验证?

java - 使用 MessagePack 的 Apache Beam - 如何从 Map<Value, Value> 获取值?

java - 用于从文件中批量插入项目的 Spring jdbc 编程事务

java - 如何使用 JSP 实现用户名可用性

java - IncorrectResultSetColumnCountException : Incorrect column count: expected 1, 实际 38

xml - spring xml 中的默认占位符值

spring - 如何从实体管理器访问 Hibernate 统计信息?

java - 如何通过类名调用方法

java - Jar 文件有类,但我仍然得到 java.lang.ClassNotFoundException : org. apache.kafka.clients.consumer.ConsumerRecord