Spring 3中的EntityManager注入

时间:2018-11-16 14:01:41

标签: java spring jpa dependency-injection tomcat7

我在尝试从Tomcat数据源注入EntityManager时遇到问题,这是我的文件:

persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">

<!-- unidade de persistencia com o nome financas -->
<persistence-unit name="cartorioDocket">

    <!-- Implementação do JPA, no nosso caso Hibernate -->
    <!-- <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> -->

    <!-- Aqui são listadas todas as entidades -->
    <class>cursos.alura.jpa.jpaComJava.model.Cartorio</class>

    <properties>    
        <!-- Propriedades JDBC -->
        <property name="javax.persistence.jdbc.driver" 
            value="com.mysql.jdbc.Driver" />

        <property name="javax.persistence.jdbc.url" 
            value="jdbc:mysql://localhost:3306/cartorio" />

        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="PASSWORD" />

        <!-- Configurações específicas do Hibernate -->
        <property name="hibernate.dialect" 
            value="org.hibernate.dialect.MySQL5InnoDBDialect" />

        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
    </properties>
</persistence-unit>

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
                    http://www.springframework.org/schema/beans 
                    http://www.springframework.org/schema/beans/spring-beans.xsd 
                    http://www.springframework.org/schema/jee 
                    http://www.springframework.org/schema/jee/spring-jee.xsd 
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="br.com.caelum.contas" />
<mvc:annotation-driven />

<!-- Create default configuration for Hibernate -->

<jee:jndi-lookup id="dbDataSource" jndi-name="java:comp/env/jdbc/cartorio" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="cartorioDocket" />
    <property name="dataSource" ref="dbDataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    </property>
</bean>

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

我试图将此@PersistenceContext(unitName = "cartorioDocket", type = PersistenceContextType.EXTENDED) private EntityManager em;添加到我的DAO类中,但是抛出了NullPointer。 当我使用JPA的标准实现时,使用Persistence.createEntityManagerFactory("cartorioDocket")可以正常工作,但不能使用注入。

0 个答案:

没有答案