2

hibernate SessionFactoryを使用すると、これを簡単に行うことができます。

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

jpa EntityManagerFactoryでそれを行うにはどうすればよいですか?

4

1 に答える 1

7

それ以上の場合もほぼ同じ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>
于 2013-01-16T21:38:57.880 に答える