您的位置:首页 > 编程语言 > Java开发

Spring 3.1 + JPA 2.0 (Hibernate 4) + MySQL 配置

2012-04-06 12:31 525 查看
近来闲暇时间摆弄了一下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)整合的配置文件:

<?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


注:最近写了一篇综合运用Spring各种相关技术的博文,涉及到技术包含了Spring 3.1.3, Spring MVC, Hibernate 4.1.7, EhCache, Spring Data JPA 1.1.0, MySQL 5, Spring Security 3.1.3,
Spring Mail, Recapcha, c3p0,博文包含了各种技术的详尽配置信息,最后给出了可运行的源码。新博文对数据相关的配置进行了更新请查阅:基于Spring的注册登录系统,配置请查看第6部分spring-data.xml配置文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息