Spring 3.1 + JPA 2.0 (Hibernate 4) + MySQL 配置 移走persistence.xml。

原文:http://blog.csdn.net/haibinzhang/article/details/7431789

   近来闲暇时间摆弄了一下Spring、Hibernate,原因非常简单,做得项目在用Java做web开发,项目使用的Jersey+EclipseLink框架。而Spring与Hiberante早已名声在外,所以可以说是慕名而来啦!接触时间不长,所以文章中有误在所难免,欢迎板砖侍候。

查看Hibernate tutorial,Hibernate提供了3种方式实现ORM:

1. Native Hibernate APIs and hbm.xml Mappings

2. Native Hibernate APIs and Annotation Mappings

3. Java Persistence API (JPA)

第一种方式对象类的定义对象与数据库间的映射配置信息是分开的,对象类就是一平常Java类,配置信息就一hbm.xml文件。用这种方式,看似划分清晰,但写起来和用起来感觉不是太爽,一是写的东西多,写的多出错的机会就大;二是不停地在对象类和配置文件间来回切换,闪得慌。第二种方式直接将hbm.xml配置文件扔掉,将配置信息以Annotation风格与对象类紧密结合在一起。整合之后感觉清爽不少,少写了不少代码,对象属性与数据库表紧密结合,代码维护成本大大减少。第三种方式采用了JPA 2.0,JPA是ORM的一种规范,定义了操作的标准接口,对应该接口已有几种实现:Hibernate,EclipseLink。与第二种方式相比,JPA方式需要一persistence.xml配置文件。

对比上述三种方式,个人比较倾向于第三种,原因一是拥有Annotation带来的优点,二来如果那天不想使用Hibernate了,改用其他类似EclipseLink,或者其他的ORM,更换代价非常小。

        Spring 3.1相比之前版本,出现了一新特征:JPA EntityManageFactory bootstrapping without persistence.xml。这一特征的出现,进一步简化了Spring与JPA间的整合:移走persistence.xml。


下面就是对Spring与JPA 2.0 (Hibernate 4)整合的配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"   
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xmlns:util="http://www.springframework.org/schema/util"  
  8.     xsi:schemaLocation="  
  9.             http://www.springframework.org/schema/beans   
  10.             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
  11.             http://www.springframework.org/schema/tx   
  12.             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
  13.             http://www.springframework.org/schema/context  
  14.             http://www.springframework.org/schema/context/spring-context-3.1.xsd  
  15.             http://www.springframework.org/schema/util   
  16.             http://www.springframework.org/schema/util/spring-util-3.1.xsd">  
  17.   
  18.   
  19.     <!-- Data Source -->  
  20.     <bean id="dataSource"  
  21.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  22.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  23.         <property name="url" value="jdbc:mysql://localhost:3306/hibernate" />  
  24.         <property name="username" value="root" />  
  25.         <property name="password" value="" />  
  26.     </bean>  
  27.   
  28.     <!-- This will ensure that Hibernate or JPA exceptions are automatically   
  29.         translated into Spring's generic DataAccessException hierarchy for those   
  30.         classes annotated with Repository. For example, see ***DAOImpl. -->  
  31.     <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />  
  32.   
  33.   
  34.     <!-- JPA Entity Manager Factory -->  
  35.     <bean id="entityManagerFactory"  
  36.         class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"  
  37.         p:packagesToScan="com.haibin.rest" p:dataSource-ref="dataSource"  
  38.         p:jpaVendorAdapter-ref="hibernateVendor" p:jpaPropertyMap-ref="jpaPropertyMap" />  
  39.   
  40.     <util:map id="jpaPropertyMap">  
  41.         <entry key="hibernate.hbm2ddl.auto" value="update" />  
  42.         <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />  
  43.   
  44.         <!-- To enable Hibernate's second level cache and query cache settings -->  
  45.         <entry key="hibernate.max_fetch_depth" value="4" />  
  46.         <entry key="hibernate.cache.use_second_level_cache" value="true" />  
  47.         <entry key="hibernate.cache.use_query_cache" value="true" />  
  48.         <entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />  
  49.     </util:map>  
  50.   
  51.     <bean id="hibernateVendor"  
  52.         class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"  
  53.         p:database="MYSQL" p:showSql="true" p:generateDdl="true"  
  54.         p:databasePlatform="org.hibernate.dialect.MySQLDialect" />  
  55.   
  56.     <!-- Transaction Config -->  
  57.     <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"  
  58.         p:entityManagerFactory-ref="entityManagerFactory">  
  59.         <property name="jpaDialect">  
  60.             <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />  
  61.         </property>  
  62.     </bean>  
  63.   
  64.     <!-- User declarative transaction management -->  
  65.     <tx:annotation-driven transaction-manager="transactionManager" />  
  66.       
  67.   
  68. </beans>  
<?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:tx="http://www.springframework.org/schema/tx" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-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/context
			http://www.springframework.org/schema/context/spring-context-3.1.xsd
			http://www.springframework.org/schema/util 
			http://www.springframework.org/schema/util/spring-util-3.1.xsd">


	<!-- Data Source -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/hibernate" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>

	<!-- This will ensure that Hibernate or JPA exceptions are automatically 
		translated into Spring's generic DataAccessException hierarchy for those 
		classes annotated with Repository. For example, see ***DAOImpl. -->
	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


	<!-- JPA Entity Manager Factory -->
	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
		p:packagesToScan="com.haibin.rest" p:dataSource-ref="dataSource"
		p:jpaVendorAdapter-ref="hibernateVendor" p:jpaPropertyMap-ref="jpaPropertyMap" />

	<util:map id="jpaPropertyMap">
		<entry key="hibernate.hbm2ddl.auto" value="update" />
		<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

		<!-- To enable Hibernate's second level cache and query cache settings -->
		<entry key="hibernate.max_fetch_depth" value="4" />
		<entry key="hibernate.cache.use_second_level_cache" value="true" />
		<entry key="hibernate.cache.use_query_cache" value="true" />
		<entry key="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
	</util:map>

	<bean id="hibernateVendor"
		class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
		p:database="MYSQL" p:showSql="true" p:generateDdl="true"
		p:databasePlatform="org.hibernate.dialect.MySQLDialect" />

	<!-- Transaction Config -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
		p:entityManagerFactory-ref="entityManagerFactory">
		<property name="jpaDialect">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
		</property>
	</bean>

	<!-- User declarative transaction management -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	

</beans>


 

配置文件主要包含了5个部分:

1.  Data source Configuration

2.  JPA Entity Manager Factory Configuration

3.  Transaction Configuration

4.  Transaction Management

5.  Exception

具体内容就不赘述了,尝试一下的话,可以参考或修改:Spring+JPA 微笑

  1. <PRE class=html name="code" sizcache="0" sizset="3"><PRE></PRE>  
  2. <PRE></PRE>  
  3. <PRE></PRE>  
  4. <PRE></PRE>  
  5. <PRE></PRE>  
  6. <PRE></PRE>  
  7. <PRE></PRE>  
  8. <PRE></PRE>  
  9. <PRE></PRE>  
  10. <PRE></PRE>  
  11. <PRE></PRE>  
  12. <PRE></PRE>  
  13. <PRE></PRE>  
  14. <PRE></PRE>  
  15.   
  16. </PRE>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值