自动扫描jpa实体的包在春天 [英] auto scan packages for jpa entities in spring
本文介绍了自动扫描jpa实体的包在春天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用hibernate SessionFactory时,可以很容易地做到这一点:
< bean id =sessionFactory
。 ..
p:packagesToScan =com.sahandrc.survey
我该怎么做使用jpa EntityManagerFactory?
解决方案
对于 Spring 3.1
这里是一个例子:
< bean id =entityManagerFactoryclass =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.0encoding =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.spr ingframework.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/>
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密码}
p:maxActive =$ {dbcp.maxActive}
p:maxIdle =$ {dbcp.maxIdle}
p:maxWait =$ {dbcp.maxWait}/ >
< util:map id =jpaPropertyMap>
< entry key =generateDdlvalue =$ {hibernate.generate_ddl}/>
< entry key =hibernate.hbm2ddl.autovalue =$ {hibernate.hbm2ddl.auto}/>
< entry key =hibernate.dialectvalue =$ {hibernate.dialect}/>
< entry key =hibernate.default_schemavalue =$ {hibernate.default_schema}/>
< entry key =hibernate.format_sqlvalue =$ {hibernate.format_sql}/>
< / util:map>
< tx:注解驱动的事务管理器=transactionManager/>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =entityManagerFactory/>
< / bean>
< / beans>
One can easily do this when using hibernate SessionFactory:
<bean id="sessionFactory"
...
p:packagesToScan="com.sahandrc.survey"
How can I do that with jpa EntityManagerFactory?
解决方案
It's almost the same thing for Spring 3.1
and higher.
Here an example :
<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" />
Example with everything in the context file :
<?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>
这篇关于自动扫描jpa实体的包在春天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文