Announcement Announcement Module
Collapse
No announcement yet.
It's trouble that use hibernate by spring? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • It's trouble that use hibernate by spring?

    I am user hibernate3.0、spring 1.2.4,
    applicatoncontext.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    
    	<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
          <value>init.properties</value>
        </property>
      	</bean>
      	<!-- Choose the dialect that matches your "dataSource" definition -->
      	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        	<property name="driverClassName">
          		<value>$&#123;datasource.driverClassName&#125;</value>
        	</property>
        	<property name="url">
         		 <value>$&#123;datasource.url&#125;</value>
        	</property>
        	<property name="username">
          		<value>$&#123;datasource.username&#125;</value>
       	 	</property>
        	<property name="password">
          		<value>$&#123;datasource.password&#125;</value>
        	</property>
        	<property name="maxActive">
          		<value>$&#123;datasource.maxActive&#125;</value>
        	</property>
        	<property name="maxIdle">
          		<value>$&#123;datasource.maxIdle&#125;</value>
        	</property>
        	<property name="maxWait">
          		<value>$&#123;datasource.maxWait&#125;</value>
        	</property>
        	<!--property name="defaultAutoCommit">
          		<value>$&#123;datasource.defaultAutoCommit&#125;</value>
        	</property-->
      	</bean>
      	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        	<property name="dataSource">
        		<ref local="dataSource"/>
        	</property>
        	<property name="mappingResources">
          		<list>
            		<value>cn/anjia66/pojo/Userdetailinfo.hbm.xml</value>
            		<value>cn/anjia66/pojo/Userinfo.hbm.xml</value>
            	</list>
            </property>
            <property name="hibernateProperties">
          		<props>
            		<prop key="hibernate.dialect">$&#123;hibernate.dialect&#125;</prop>
            		<prop key="hibernate.show_sql">$&#123;hibernate.show_sql&#125;</prop>
            		<prop key="hibernate.jdbc.fetch_size">$&#123;hibernate.jdbc.fetch_size&#125;</prop>
            		<prop key="hibernate.jdbc.batch_size">$&#123;hibernate.jdbc.batch_size&#125;</prop>
          		</props>
        	</property>
      	</bean>
     	
      	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    		<property name="sessionFactory" ref="sessionFactory"/>
    	</bean>
    	
    	<bean id="userInfoService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager"/>
    		<property name="target" ref="userInfoServiceTarget"/>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="saveUser">PROPAGATION_NESTED</prop>
    				<prop key="createUser">PROPAGATION_REQUIRED,readOnly</prop>
    			</props>
    		</property>
    	</bean>
      		
      	  	
      	<bean id="userInfoDao" class="cn.anjia66.dao.hibernate.UserInfoHibernateDao">
      		<property name="sessionFactory">
      			<ref local="sessionFactory"/>
      		</property>
      	</bean>
      	
      	  	
      	<bean id="globeContext" class="cn.anjia66.spring.util.GlobeContext"/>
      	
      	
      	
      	<bean id="userDetailInfoDao" class="cn.anjia66.dao.hibernate.UserDetailInfoHibernateDao">
      		<property name="sessionFactory">
      			<ref local="sessionFactory"/>
      		</property>
      	</bean>
      	
      	<bean id="userInfoServiceTarget" class="cn.anjia66.business.impl.UserInfoServiceImpl">
      		<property name="userInfoDao">
      			<ref local="userInfoDao"/>
      		</property>
      		<property name="userDetailInfoDao">
      			<ref local="userDetailInfoDao"/>
      		</property>
      	</bean>
      	
      	  
    </beans>
    IUserInfoDao.java
    Code:
    package cn.anjia66.dao;
    
    import cn.anjia66.pojo.Userinfo;
    
    public interface IUserInfoDao &#123;
    
    	public Userinfo saveUserInfo&#40;Userinfo ui&#41;;
    		
    &#125;
    IUserDetailInfoDao.java
    Code:
    package cn.anjia66.dao;
    
    import cn.anjia66.pojo.Userdetailinfo;
    
    public interface IUserDetailInfoDao &#123;
    
    	public Userdetailinfo saveUserDetailInfo&#40;Userdetailinfo ud&#41;;
    &#125;
    UserInfoHibernateDao.java
    Code:
    package cn.anjia66.dao.hibernate;
    
    import cn.anjia66.dao.IUserInfoDao;
    import cn.anjia66.pojo.Userinfo;
    
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.dao.DataAccessException;
    
    public class UserInfoHibernateDao extends HibernateDaoSupport implements
            IUserInfoDao &#123;
    
        public Userinfo saveUserInfo&#40;Userinfo ui&#41; throws DataAccessException &#123;
    
            getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;ui&#41;;
            return ui;
    
        &#125;
    
    &#125;
    UserDetailInfoHibernateDao.java
    Code:
    package cn.anjia66.dao.hibernate;
    
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
    import org.springframework.dao.DataAccessException;
    import cn.anjia66.dao.IUserDetailInfoDao;
    import cn.anjia66.pojo.Userdetailinfo;
    
    public class UserDetailInfoHibernateDao extends HibernateDaoSupport implements
            IUserDetailInfoDao &#123;
    
        public Userdetailinfo saveUserDetailInfo&#40;Userdetailinfo ud&#41;
                throws DataAccessException &#123;
    
            getHibernateTemplate&#40;&#41;.saveOrUpdate&#40;ud&#41;;
            return ud;
    
        &#125;
    
    &#125;
    IUserInfoService.java
    Code:
    package cn.anjia66.business;
    
    import cn.anjia66.pojo.Userinfo;
    import cn.anjia66.pojo.Userdetailinfo;
    
    public interface IUserInfoService &#123;
    
    	public Userinfo saveUserInfo&#40;Userinfo ui&#41;;
    	
    	public boolean saveUser&#40;Userinfo ui,Userdetailinfo ud&#41;;
    	public boolean createUser&#40;Userinfo ui,Userdetailinfo ud&#41;;
    	
    &#125;
    UserInfoServiceImpl.java
    Code:
    package cn.anjia66.business.impl;
    
    import org.springframework.dao.DataAccessException;
    import org.springframework.dao.DataRetrievalFailureException;
    
    import cn.anjia66.business.IUserInfoService;
    import cn.anjia66.dao.IUserDetailInfoDao;
    import cn.anjia66.dao.IUserInfoDao;
    import cn.anjia66.pojo.Userdetailinfo;
    import cn.anjia66.pojo.Userinfo;
    
    public class UserInfoServiceImpl implements IUserInfoService &#123;
    
        private IUserInfoDao userInfoDao;
    
        private IUserDetailInfoDao userDetailInfoDao;
    
        public IUserDetailInfoDao getUserDetailInfoDao&#40;&#41; &#123;
            return userDetailInfoDao;
        &#125;
    
        public void setUserDetailInfoDao&#40;IUserDetailInfoDao userDetailInfoDao&#41; &#123;
            this.userDetailInfoDao = userDetailInfoDao;
        &#125;
    
        public void setUserInfoDao&#40;IUserInfoDao userInfoDao&#41; &#123;
            this.userInfoDao = userInfoDao;
        &#125;
    
        public IUserInfoDao getUserInfoDao&#40;&#41; &#123;
            return this.userInfoDao;
        &#125;
    
        public Userinfo saveUserInfo&#40;Userinfo ui&#41; &#123;
            return getUserInfoDao&#40;&#41;.saveUserInfo&#40;ui&#41;;
        &#125;
    
        public Userdetailinfo savaUserDetailInfo&#40;Userdetailinfo ud&#41; &#123;
            return getUserDetailInfoDao&#40;&#41;.saveUserDetailInfo&#40;ud&#41;;
        &#125;
    
        public boolean saveUser&#40;Userinfo ui, Userdetailinfo ud&#41;&#123;
            ui = this.saveUserInfo&#40;ui&#41;;
            ud.setUserid&#40;ui.getId&#40;&#41;&#41;;        
            createUser&#40;ui, ud&#41;;
            this.savaUserDetailInfo&#40;ud&#41;;
            return true;
        &#125;
        
        public boolean createUser&#40;Userinfo ui, Userdetailinfo ud&#41;&#123;
            ui = this.saveUserInfo&#40;ui&#41;;
            ud.setUserid&#40;ui.getId&#40;&#41;&#41;;
            this.savaUserDetailInfo&#40;ud&#41;;
            //throw new DataRetrievalFailureException&#40;"test"&#41;;
            return true;
        &#125;
    &#125;
    TestUserInfoService.java
    Code:
    package cn.anjia66.business;
    
    import junit.framework.TestCase;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import cn.anjia66.pojo.Userdetailinfo;
    import cn.anjia66.pojo.Userinfo;
    
    public class TestUserInfoService extends TestCase &#123;
        private IUserInfoService uiService;
    
        /*
         * @see TestCase#setUp&#40;&#41;
         */
        protected void setUp&#40;&#41; throws Exception &#123;
            super.setUp&#40;&#41;;
            ApplicationContext appContext = new ClassPathXmlApplicationContext&#40;
                    "applicationContext.xml"&#41;;
            uiService = &#40;IUserInfoService&#41; appContext.getBean&#40;"userInfoService"&#41;;
        &#125;
    
        /*
         * @see TestCase#tearDown&#40;&#41;
         */
        protected void tearDown&#40;&#41; throws Exception &#123;
            super.tearDown&#40;&#41;;
        &#125;
    
        public void testSaveUser&#40;&#41; &#123;
            Userinfo ui = new Userinfo&#40;&#41;;
            Userdetailinfo ud = new Userdetailinfo&#40;&#41;;
            ui.setName&#40;"david"&#41;;
            ui.setSex&#40;"man"&#41;;
    
            ud.setAge&#40;Integer.valueOf&#40;28&#41;&#41;;
            ud.setHeight&#40;Integer.valueOf&#40;178&#41;&#41;;
            uiService.saveUser&#40;ui, ud&#41;;
            // uiService.createUser&#40;ui,ud&#41;;
        &#125;  
    
    &#125;
    I did test Case to testSaveUser() method,
    I expect that throw exception ,rollback transactions and no data record inserted into the DB,but have not throw exception and the only one data record inserted int the DB,other data record did not inserted into the DB?
    what's wrong here?


    other,
    I modified UserInfoServiceImpl.java ,follow:
    Code:
    package cn.anjia66.business.impl;
    
    import org.springframework.dao.DataAccessException;
    import org.springframework.dao.DataRetrievalFailureException;
    
    import cn.anjia66.business.IUserInfoService;
    import cn.anjia66.dao.IUserDetailInfoDao;
    import cn.anjia66.dao.IUserInfoDao;
    import cn.anjia66.pojo.Userdetailinfo;
    import cn.anjia66.pojo.Userinfo;
    
    public class UserInfoServiceImpl implements IUserInfoService &#123;
    
        private IUserInfoDao userInfoDao;
    
        private IUserDetailInfoDao userDetailInfoDao;
    
        public IUserDetailInfoDao getUserDetailInfoDao&#40;&#41; &#123;
            return userDetailInfoDao;
        &#125;
    
        public void setUserDetailInfoDao&#40;IUserDetailInfoDao userDetailInfoDao&#41; &#123;
            this.userDetailInfoDao = userDetailInfoDao;
        &#125;
    
        public void setUserInfoDao&#40;IUserInfoDao userInfoDao&#41; &#123;
            this.userInfoDao = userInfoDao;
        &#125;
    
        public IUserInfoDao getUserInfoDao&#40;&#41; &#123;
            return this.userInfoDao;
        &#125;
    
        public Userinfo saveUserInfo&#40;Userinfo ui&#41; &#123;
            return getUserInfoDao&#40;&#41;.saveUserInfo&#40;ui&#41;;
        &#125;
    
        public Userdetailinfo savaUserDetailInfo&#40;Userdetailinfo ud&#41; &#123;
            return getUserDetailInfoDao&#40;&#41;.saveUserDetailInfo&#40;ud&#41;;
        &#125;
    
        public boolean saveUser&#40;Userinfo ui, Userdetailinfo ud&#41;&#123;
            ui = this.saveUserInfo&#40;ui&#41;;
            ud.setUserid&#40;ui.getId&#40;&#41;&#41;;        
            createUser&#40;ui, ud&#41;;
           throw new DataRetrievalFailureException&#40;"test"&#41;;
            return true;       
        &#125;
        
        public boolean createUser&#40;Userinfo ui, Userdetailinfo ud&#41;&#123;
            ui = this.saveUserInfo&#40;ui&#41;;
            ud.setUserid&#40;ui.getId&#40;&#41;&#41;;
            this.savaUserDetailInfo&#40;ud&#41;;
            //throw new DataRetrievalFailureException&#40;"test"&#41;;
            return true;
        &#125;
    &#125;
    and modified applicationcontext.xml,follow partly code:
    Code:
    <bean id="userInfoService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    		<property name="transactionManager" ref="transactionManager"/>
    		<property name="target" ref="userInfoServiceTarget"/>
    		<property name="transactionAttributes">
    			<props>
    				<prop key="saveUser">PROPAGATION_REQUIRED</prop>				
    			</props>
    		</property>
    	</bean>
    I did test case to testSaveUser() method,
    I expect that throw exception ,rollback transactions and no data record inserted into the DB,
    but only have throw exception and the data record inserted into the DB?
    what's wrong right here?

    Thank you for anywhere refrence.
    Regard.
    David

  • #2
    I already solved this problem,because used the mysql db.
    the mysql db need to support trasaction ,must make the type of table to the 'InnoDB' type.

    Comment

    Working...
    X