Announcement Announcement Module
Collapse
No announcement yet.
Spring,JUnit, Hibernate and persistence db but not insert Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring,JUnit, Hibernate and persistence db but not insert

    This is my spring-master.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
             <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    	<context:property-placeholder location="classpath*:META-INF/spring/*.properties" />
    	
    
    	<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    	<!--Component, @Repository, and @Service -->
    	<context:component-scan base-package="com.reglamb">
         <!--avoid instantiating our @Controller classes -->
         <context:exclude-filter type="annotation"
    	expression="org.springframework.stereotype.Controller" />
         <context:exclude-filter expression="org.springframework.stereotype.Repository" type="annotation" />
    	</context:component-scan>
    	<context:component-scan base-package="com.reglamb.projvehi" />
    	<import resource="spring-datasource.xml" />
    
    	<import resource="spring-hibernate.xml" />
    
        </beans>
    my spring-datasource.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                               http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    
        <bean id="vehicDataSource"
     	class="org.springframework.jdbc.datasource.DriverManagerDataSource"
     	p:driverClassName="${jdbc.driverClassName}"
     	p:url="${jdbc.url}"
     	p:username="${jdbc.username}"
     	p:password="${jdbc.password}"/>
      
        </beans>
    and my spring-hibernate.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                          http://www.springframework.org/schema/context
                          http://www.springframework.org/schema/context/spring-context-3.0.xsd
                          http://www.springframework.org/schema/tx
                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
        <!-- Hibernate SessionFactory -->
        <bean id="sessionFactory" 
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
    	p:dataSource-ref="vehicDataSource"
    	p:lobHandler-ref="defaultLobHandler">
    	<property name="annotatedClasses">
    		<list>
    		<value>com.reglamb.projvehi.domain.Solicitante</value>
    		</list>
    	</property>
    	<property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.jdbc.batch_size=${hibernate.jdbc.batch_size}
                    hibernate.c3p0.max_size=${hibernate.c3p0.max_size}
                    hibernate.c3p0.min_size=${hibernate.c3p0.min_size}
                    hibernate.c3p0.timeout=${hibernate.c3p0.timeout}
                    hibernate.c3p0.max_statements=${hibernate.c3p0.max_statements}
                    hibernate.c3p0.idle_test_period=${hibernate.c3p0.idle_test_period}
                    hibernate.c3p0.acquire_increment=${hibernate.c3p0.acquire_increment}
                    hibernate.c3p0.validate=${hibernate.c3p0.validate}
                    <!--hibernate.cache.provider_class=${hibernate.cache.provider_class}-->
                    hibernate.connection.provider_class=${hibernate.connection.provider_class}
                    hibernate.show_sql=${hibernate.show_sql}
                    hibernate.hbm2ddl.auto=update
                    <!--hibernate.cache.use_second_level_cache=true-->
                    <!--hibernate.cache.use_query_cache=true-->
                 </value>
            </property>
        </bean>
        <!-- Database LOB Handling -->
        <bean id="defaultLobHandler"   class="org.springframework.jdbc.support.lob.DefaultLobHandler"/>
    
    	 <!-- Transaction Config -->
         <bean id="transactionManager"
              class="org.springframework.orm.hibernate3.HibernateTransactionManager"
              p:sessionFactory-ref="sessionFactory"/>   
    
         <bean id="hibernateTemplate" 
     	class="org.springframework.orm.hibernate3.HibernateTemplate" 
     	p:sessionFactory-ref ="sessionFactory"/>
     
         <bean id="solicitanteDao" 
    	class="com.reglamb.projvehi.dao.hibernate.SolicitanteDaoHibernate" > 
    	<property name="hibernateTemplate" ref ="hibernateTemplate" />
         </bean>
     
         <tx:annotation-driven transaction-manager="transactionManager"/>
     
         </beans>
    My Dao Class and Imp :
    Code:
    import java.io.Serializable;
             import java.util.List;
             import com.reglamb.projvehi.domain.Solicitante;
    
          public interface SolicitanteDao extends GenericDao<Solicitante>,Serializable{
    
    	   public void saveUser(Solicitante solicitante);
    	   public void deleteUser(String solicitante) ;
    	   public List<Solicitante> getAllUser(Solicitante solicitante) ;
    	   public Solicitante selectUserById(String solicId) ;
    	
             }
    Code:
    <!-----------------Impl------------------------------------------>
           @Repository("solicitanteDao")
           public class SolicitanteDaoHibernate extends HibernateDaoSupport implements SolicitanteDao{
             private static final long serialVersionUID = 1L;
    
    
    	//@Transactional(readOnly = false)
    	//@Transactional
    	public void saveUser(Solicitante solicitante) {
    		 getHibernateTemplate().saveOrUpdate(solicitante);
    
    	}
    
    
    	public void deleteUser(String solicitante) {
    		getHibernateTemplate().delete(solicitante);
    		
    	}
    
    	@SuppressWarnings("unchecked")
    	//@Transactional
    	public List<Solicitante> getAllUser(Solicitante solicitante) {
    		return (List<Solicitante>) getHibernateTemplate().find("from "
    				                + Solicitante.class.getName());
    	}
    	
    	
    	public Solicitante selectUserById(String solicId) {
    		return getHibernateTemplate().get(Solicitante.class, solicId);
    	}
    I want to probe this by Junit like this:
    Code:
     @RunWith(SpringJUnit4ClassRunner.class)
         @ContextConfiguration(locations = { 
    			"classpath:/META-INF/spring/spring-master.xml"})
         @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
         @Transactional()
    	public class DataJUnitTest {
    	@Autowired
    	SolicitanteDao solicitanteDao;
    		
    
    	@Test
    	public void testCreateData() {
    	    Solicitante solic = new Solicitante();
    	    solic.setLastname("AYAMBO");
    	    solic.setName("TOLEDO");
    	    System.out.println("sOMETHING ???");
    	    solicitanteDao.saveUser(solic);
    	    Assert.assertEquals(1, solicitanteDao.getAllUser(new Solicitante()).size());
    		}
    	}
    Run this by Junit test and take this:
    Code:
    Running com.reglamb.projvehi.test.DataJUnitTest
          SOMETHING ???
          Hibernate: select nextval ('hibernate_sequence')
          Hibernate: insert into Solicitante (lastname, name, solicId) values (?, ?, ?)
          Hibernate: select solicitant0_.solicId as solicId0_, solicitant0_.lastname as lastname0_, solicitant0_.name as name0_ from Solicitante solicitant0_
           Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.906 sec
    The database is create but the data id-generated,ayambo,toledo not inserted, the database is Postgresql 9.0.3, Pls?, What i do wrong???
    Pdta: I now that hibernatetemplate is unused, but the configuration is ok??(Sorry for my bad english.)

  • #2
    JUnit test perform a rollback when test finish... I guess it is wwhat you have.

    Comment


    • #3
      Thanks gwa, I solve this change in the Junit
      @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false),
      Thnks

      Comment

      Working...
      X