日期:2014-05-16  浏览次数:20485 次

hibernate 基于cglib的 事务代理
    实例:使用hibernate 事务代理完成一个事务,添加一个人,这个人有两个地址。这是一个事务,要么全部插进去,要么谁也不插进去。

1,Person类
package com.dd.domain;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Person {
	private int id;
	private String name;
	private int age;
    private Set<Address> addresss = new HashSet<Address>();
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Set<Address> getAddresss() {
		return addresss;
	}
	public void setAddresss(Set<Address> addresss) {
		this.addresss = addresss;
	}
}


Person类的映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="true" default-access="property" package="com.dd.domain" >
    <class name="Person" dynamic-update="true" dynamic-insert="true" table="myperson">
        <id name="id" column="id" type="integer">
            <generator class="native"/>
        </id>
        <property name="name" type="string" column="name" length="20"/>
        <property name="age" type="integer" column="age" length="3"/>
        <set name="addresss" inverse="true" cascade="all" >
             <key column="personid"/>
             <one-to-many class="Address"/>
        </set>
    </class>
</hibernate-mapping>


2,Address类
package com.dd.domain;
public class Address {
	private int id;
	private String name;
	private String info;
    private String remark;
    private Person person;
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
}

Address类映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="true" default-access="property" package="com.dd.domain" >
    <class name="Address" dynamic-update="true" dynamic-insert="true" table="address">
        <id name="id" column="id" type="integer">
            <generator class="native"/>
        </id>
        <property name="name" type="string" column="NAME" length="20"/>
        <property name="info" type="string" column="INFO" length="30"/>
        <property name="remark" type="string" column="REMARK" length="30"/>
        <many-to-one name="person" class="Person" column="personid" not-null="true"></many-to-one>
    </class>
</hibernate-mapping>

4,Sprping配置文件
    <!-- Person业务类的事务代理,业务中实际用的是这个代理类 -->
    <bean id="myPersonServiceProxy" parent="baseTransactionProxy">
    <property name="target">
       <ref bean="personService"/>
    </property>
    </bean> 
    
    <!-- 真是的PersonService类,实际业务中没有用到这个类,而是这个类的代理类 -->
    <bean id="personService" class="com.dd.service.PersonServiceImpl">
        <property name="personDao" ref="personDao2"></property>
        <property name