1

我正在设置一个 maven-spring-hibernate-mysql-tomcat 环境。

我想使用 @PersistenceContext 注释注入我的实体管理器。据我所知,我应该在我的应用程序上下文中定义 PersistenceAnnotationBeanPostProcessor,指向我的 persistence.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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:ox="http://www.springframework.org/schema/oxm"
    xsi:schemaLocation="
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
   http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-3.1.xsd">

    <!-- Root Context: defines shared resources visible to all other web components -->

    <!-- Context -->
    <context:component-scan base-package="com.yl.mypack" />

    <!-- AOP -->
    <aop:aspectj-autoproxy />

    <!-- Properties -->

    <context:property-placeholder
        location="classpath:db.connection.properties,applicationProperties.properties" />

    <!-- DB Connection Pool -->
    <bean id="dataSourceGlobal" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        destroy-method="close">

        <!-- Data Source -->
        <property name="driverClass" value="${driverClass}" />
        <property name="jdbcUrl" value="${jdbcUrl}" />
        <property name="user" value="${user}" />
        <property name="password" value="${password}" />

        <!-- C3P0 Connection pool properties -->
        <property name="minPoolSize" value="${c3p0.min_pool_size}" />
        <property name="maxPoolSize" value="${c3p0.max_pool_size}" />
        <property name="unreturnedConnectionTimeout" value="${c3p0.timeout}" />
        <property name="idleConnectionTestPeriod" value="${c3p0.idle_test_period}" />
        <property name="maxStatements" value="${c3p0.max_statements}" />
        <property name="automaticTestTable" value="${c3p0.automatic_test_table}" />
    </bean>

    <!-- JPA -->
    <!-- Creates a EntityManagerFactory for use with the Hibernate JPA provider -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
            <property name="persistenceUnitName" value="myPU" />
            <property name="dataSource" ref="dataSourceGlobal" />
    </bean>



    <!-- In order to enable EntityManager injection -->
    <bean id="persistenceAnnotation"
        class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
        <property name="persistenceUnits">
            <map>
                <entry key="myPU" value="classpath:META-INF/persistence.xml" />
            </map>
        </property>
    </bean>

    <!-- Transactions -->
    <tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSourceGlobal" />
    </bean>

</beans>

我的 persistence.xml 可能如下所示:

<persistence-unit name="myPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.transaction.flush_before_completion"
            value="true" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

所以现在我想我应该能够使用 @PersistenceContext 注释注入我的实体管理器,在列表中这是我从文档中得到的。在我进入 JpaVendorAdapter 之前,我的第一个问题是实体管理器工厂定义:以下属性:

        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        <property name="persistenceUnitName" value="myPU" />

这些是否需要从实体管理器工厂转介?这是必须的吗?

我的第二个问题是关于 JpaVendorAdapter,在我的例子中是HibernateJpaVendorAdapter

如果我使用 HibernateJpaVendorAdapter,我还需要一个 persistence.xml 文件吗?由于某些属性是重叠的。此外,我还需要定义 PersistenceAnnotationBeanPostProcessor,指向我的 persistence.xml 文件吗?这两个可以一起使用吗(PersistenceAnnotationBeanPostProcessor 和 HibernateJpaVendorAdapter)?他们应该一起去吗?假设我避免使用任何 JDNI 样式定义,最佳实践是什么?

提前致谢

4

1 回答 1

0

数据源 bean 和 HibernateJpaVendorAdapter 的结合使您可以在 Spring 3.1 中摆脱 persistence.xml。看看这篇文章:

http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/

所以答案:

  1. 它们不是必须的。
  2. 不,您不需要有 persistence.xml。
  3. 我不确定 PersistenceAnnotationBeanPostProcessor 的用途是什么。

    <context:component-scan base-package="com.demo.dao" />
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="hibernate-resourceLocal"/>
    </bean>
    
    <tx:annotation-driven/>
    
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    

我的 Spring 应用程序上下文 xml 看起来就这么简单。它正在工作。

我不知道最佳实践是什么,但我会保留 persistence.xml,以便在需要将我的服务公开为会话 bean 时,可以轻松完成。

于 2012-09-18T04:43:18.347 回答