spring+hibernate整と配置における宣言的事物管理


spring+hibernate整と配置の中で宣言式の事物は管理して、事物は代行して策略を配置します。
 
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

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

	<!--    hibernate SessionFactory-->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource"/></property>
        <property name="mappingResources">
            <list>
                <!--         PO    -->
				<value>Person.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
        <props>
            <!--      hibernate SessionFactory   :       ,     create,update,create-drop-->
			<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
        </property>
    </bean>

    <!--         -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <!--  HibernateTransactionManager  bean        SessionFactory bean   -->
    	<property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    <!--         -->
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <!--       bean              -->
        <property name="transactionManager" ref="transactionManager"/>
        <property name="transactionAttributes">
            <!--            -->
            <props>
                <prop key="insert*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
	</bean>


    <!--   BeanNameAutoProxyCreator, bean  bean    ,     ,    id  -->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<!--         bean name bean         -->
	    <property name="beanNames">
            <!--                  bean-->
            <list>
                <value>personDao</value>
            </list>
            <!--                    bean-->
	    </property>
        <!--      BeanNameAutoProxyCreator        -->
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value> 
                    <!--          Interceptor -->
            </list>
        </property>
    </bean>



    <!--  DAO Bean ,  BeanNameAutoProxyCreator        -->
	<bean id="personDao" class="lee.PersonDaoHibernate">
        <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>


</beans>
テストコード
package lee;

import java.util.Collection;
import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DataSourceUtils;

/**
 * @author  yeeku.H.lee [email protected]
 * @version  1.0
 * <br>Copyright (C), 2005-2008, yeeku.H.Lee
 * <br>This program is protected by copyright laws.
 * <br>Program Name:
 * <br>Date: 
 */

//       MPersonDao  ,     ,           。
public class PersonDaoHibernate extends HibernateDaoSupport implements PersonDao
{
    /**
      *      
      * @param id      Person      
      * @return      Person  
      */ 
    public Person get(int id)
    {
        return (Person)getHibernateTemplate().get(Person.class, new Integer(id)); 
    }

    /**
      *      
      * @param person      Person  
      */    
    public void save(Person person)
    {
        getHibernateTemplate().save(person);
        DataSource ds = null;
        DataSourceUtils.getConnection(ds);
    }

    /**
      *   Person  
      * @param person      Person  
      */
    public void update(Person person)
    {
        getHibernateTemplate().update(person);
    }

    /**
      *   Person  
      * @param id      Person id
      */
    public void delete(int id)
    {
        getHibernateTemplate().delete(getHibernateTemplate().get(Person.class, new Integer(id)));
    }

    /**
      *   Person  
      * @param person      Person  
      */
    public void delete(Person person)
    {
        getHibernateTemplate().delete(person);
    }

    /**
      *        Person
      * @param name    
      * @return           
      */
    public List findByPerson(String name)
    {
        return getHibernateTemplate().find("from Person p where p.name like ?" , name);        
    }

    /**
	  *      Person  
	  * @return    Person  
	  */
    public List findAllPerson()
    {
        return getHibernateTemplate().find("from Person ");     
    }
}