spring - spring 中 jpa 实体的自动扫描包

标签 spring hibernate jpa

使用 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>

关于spring - spring 中 jpa 实体的自动扫描包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14368119/

相关文章:

java - Spring中如何使用Tomcat提供的JNDI DataSource?

java - 为 oxm jaxb2 spring 服务获取 406。编码请求体有效,返回响应体无效

java - 如何通过hibernate检查数据库是否启动?

java - JPA 试图强制我拥有一个名为 'id' 的属性和数据库列

spring - javax.persistence.TransactionRequiredException : Executing an update/delete query

java - 如何使用 Lombok 和 Spring Boot 访问 JPA 实体的 Builder()?

html - 使用 SpringFramework3 制作时事通讯(HTML)

java - Spring - 使用嵌套实体发布 JSON 主体不起作用

java - Spring Hibernate getCurrentSession() 当没有 session 时?

java - 首次发布应用程序时,持久性单元无法注入(inject),但重新启动应用程序时注入(inject)成功