以下几篇文章简单的介绍一下jpa 和 spring2.5 hibernate3.2 整合配置的一个过程。纯粹个人经验只谈。如果有错误,请各位留言指出。
本系列重点是涉及 配置过程 ,对注释的用法不多介绍。
注 释语法越来越多的被业界所使用,并且注释配置相对于 XML 配置具有很多的优势:它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作。注释和 Java 代码位于一个文件中,而 XML 配置采用独立的配置文件,大多数配置信息在程序开发完成后都不会调整,如果配置信息和 Java 代码放在一起,有助于增强程序的内聚性。而采用独立的 XML 配置文件,程序员在编写一个功能时,往往需要在程序文件和配置文件中不停切换,这种思维上的不连贯会降低开发效率。因此在很多情况下,注释配置比 XML 配置更受欢迎,注释配置有进一步流行的趋势。Spring 2.5 的一大增强就是引入了很多注释类,现在您已经可以使用注释配置完成大部分 XML 配置的功能。
首先,我们已经通过 传统的spring +hibernate方式构架成功了一个应用的后台体系。
这个体系里面 有这样几个重要的配置文件。
- hibernate.cfg.xml 。
里面通过 配置 mapping来指向每张数据表单生成配置文件.xxxx.hbm.xml文件 - applicaitonContex.xml。
里面通过定义一个一个bean 来配置 各个需要用到的 DAO 和 Service。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans > <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <!-- 事务拦截器bean需要依赖注入一个事务管理器 --> <property name="transactionManager"> <ref local="transactionManager"/> </property> <property name="transactionAttributes"> <!-- 下面定义事务传播属性--> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> <prop key="delete*">PROPAGATION_REQUIRED</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="change*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean> <!-- 定义自动代理BeanNameAutoProxyCreator --> <bean id="beanNameAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <!-- 指定对满足哪些bean name的bean自动生成业务代理 --> <property name="beanNames"> <list> <value>*Service</value> </list> </property> <!-- 下面定义BeanNameAutoProxyCreator所需的事务拦截器--> <property name="interceptorNames"> <list> <!-- 此处可增加其他新的Interceptor --> <value>transactionInterceptor</value> </list> </property> </bean> <bean id="McCityInfoDAO" class="com.firemax.manatee.hibernate.McCityInfoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> <bean id="McMaterialInfoDAO" class="com.firemax.manatee.hibernate.McMaterialInfoDAO"> <property name="sessionFactory"> <ref bean="sessionFactory" /> </property> </bean> </beans>
- 首先我们需要
- hibernate3.2 以上版本的jar
- jdk 5 以上的环境
- spring2
然后我们修改pojo的java类。加上注解。使起通过注解来取代原先要xxxx.hbm.xml里面配置的指向的数据库表单结构的信息。
package com.alcor.web.hibernate;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* AlcorTCitys entity. @author MyEclipse Persistence Tools
*/
@Entity
@Table(name = "alcor_t_citys", catalog = "alcorweb")
public class AlcorTCitys implements java.io.Serializable {
// Fields
private String cityCode;
private AlcorTProvinces alcorTProvinces;
private String cityName;
private Set<AlcotTDistrict> alcotTDistricts = new HashSet<AlcotTDistrict>(0);
// Constructors
/** default constructor */
public AlcorTCitys() {
}
/** minimal constructor */
public AlcorTCitys(String cityCode, AlcorTProvinces alcorTProvinces,
String cityName) {
this.cityCode = cityCode;
this.alcorTProvinces = alcorTProvinces;
this.cityName = cityName;
}
/** full constructor */
public AlcorTCitys(String cityCode, AlcorTProvinces alcorTProvinces,
String cityName, Set<AlcotTDistrict> alcotTDistricts) {
this.cityCode = cityCode;
this.alcorTProvinces = alcorTProvinces;
this.cityName = cityName;
this.alcotTDistricts = alcotTDistricts;
}
// Property accessors
@Id
@Column(name = "city_code", unique = true, nullable = false, length = 32)
public String getCityCode() {
return this.cityCode;
}
public void setCityCode(String cityCode) {
this.cityCode = cityCode;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "province_code", nullable = false)
public AlcorTProvinces getAlcorTProvinces() {
return this.alcorTProvinces;
}
public void setAlcorTProvinces(AlcorTProvinces alcorTProvinces) {
this.alcorTProvinces = alcorTProvinces;
}
@Column(name = "city_name", nullable = false, length = 64)
public String getCityName() {
return this.cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "alcorTCitys")
public Set<AlcotTDistrict> getAlcotTDistricts() {
return this.alcotTDistricts;
}
public void setAlcotTDistricts(Set<AlcotTDistrict> alcotTDistricts) {
this.alcotTDistricts = alcotTDistricts;
}
}
- 修改hibernate.cfg.xml中的定义方式,把原来的maping source=“xxxxx.hbm.xml”修改成
这样我们就可以把原来的 xxxxx.hbm.xml全部删除了。
经过这个步骤。如果你原有的servcice层的功能能够正常使用。恭喜你。迈出了成功的第一步。