使用 hibernate SessionFactory 时可以很容易地做到这一点:

<bean id="sessionFactory" 
... 
p:packagesToScan="com.sahandrc.survey"  

我如何使用 jpa EntityManagerFactory 做到这一点?

请您参考如下方法:

Spring 3.1 和更高版本几乎是一样的。

举个例子:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
            p:packagesToScan="com.sahandrc.survey" 
            p:dataSource-ref="dataSource" 
            p:jpaPropertyMap-ref="jpaPropertyMap" 
            p:jpaVendorAdapter-ref="hibernateVendor" /> 

上下文文件中所有内容的示例:

<?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:tx="http://www.springframework.org/schema/tx" 
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 
 
    <context:property-placeholder 
        location="WEB-INF/database.properties"/> 
 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
        p:packagesToScan="com.sahandrc.survey" 
        p:dataSource-ref="dataSource" 
        p:jpaPropertyMap-ref="jpaPropertyMap" 
        p:jpaVendorAdapter-ref="hibernateVendor" /> 
 
    <bean id="dataSource" 
          class="org.apache.commons.dbcp.BasicDataSource" 
          destroy-method="close" 
          p:driverClassName="${jdbc.driverClassName}" 
          p:url="${jdbc.url}" 
          p:username="${jdbc.username}" 
          p:password="${jdbc.password}" 
          p:maxActive="${dbcp.maxActive}" 
          p:maxIdle="${dbcp.maxIdle}" 
          p:maxWait="${dbcp.maxWait}"/> 
 
    <util:map id="jpaPropertyMap"> 
        <entry key="generateDdl" value="${hibernate.generate_ddl}"/> 
        <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl.auto}"/> 
        <entry key="hibernate.dialect" value="${hibernate.dialect}"/> 
        <entry key="hibernate.default_schema" value="${hibernate.default_schema}"/> 
        <entry key="hibernate.format_sql" value="${hibernate.format_sql}"/> 
    </util:map> 
 
    <tx:annotation-driven transaction-manager="transactionManager" /> 
 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
        <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 
 
</beans> 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!