Announcement Announcement Module
Collapse
No announcement yet.
Lazy collections not "touched" in service Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Lazy collections not "touched" in service

    Hi everyone,

    I'am trying to deal with an issue since one week.

    I have a Service , say a class using one DAO.

    This class is proxied by
    Code:
    org.springframework.transaction.interceptor.TransactionProxyFactoryBean
    In a method (findUser), I use a businessObject (advert) in which I call a method named getOwner(). This method calls a set (getTransactions()).

    AdvertService.java :
    Code:
    public class AdvertService implements IAdvertService {
    	public Users findUser(Advert advert) {
    		if (advert == null || advert.getId() == null) throw new IllegalArgumentException("advert or advert.id is null");
    		return advert.getOwner();
    	}
    }
    IAdvertService.java :
    Code:
    public interface IAdvertService {
    	Advert findUser(Advert advert);
    }
    Advert.java :
    Code:
    public abstract class Advert implements Serializable {
            // Other methods omitted
        private Set transactions;
    
        public Set getTransactions() {
            return this.transactions;
        }
    
        public void setTransactions(Set transactions) {
            this.transactions = transactions;
        }
    
    	public Users getOwner() {
    		Users owner = null;
    		if (this.getTransactions() != null && this.getTransactions().size() > 0)
    			owner = ((Transaction)this.getTransactions().iterator().next()).getUsers();
    		return owner;
    	}
    }
    Advert.hbm.xml
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
        
    <hibernate-mapping>
    <class 
        name="com.company.dreams.domainmodel.Advert" 
        table="ADVERT"
    >
        <id
            name="id"
            type="java.lang.Integer"
            column="ID"
        >
            <generator class="native" />
        </id>
        <set
            name="transactions"
            lazy="true"
    		cascade="all"
        >
            <key>
                <column name="ADVERT_ID" />
            </key>
            <one-to-many 
                class="com.company.dreams.domainmodel.Transaction"
            />
        </set>
    </class>
    </hibernate-mapping>
    applicationContext.xml
    Code:
    <bean id="advertService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">	
    		<property name="transactionManager"><ref local="tm"/></property>
    		<property name="target"><ref local="advertTarget"/></property>
    		<property name="transactionAttributes">
    			<props>
    	 			<prop key="save*">PROPAGATION_REQUIRED</prop>
    				<prop key="update*">PROPAGATION_REQUIRED</prop>
    				<prop key="delete*">PROPAGATION_REQUIRED</prop>
    				<prop key="findUser">PROPAGATION_REQUIRED</prop>
    			</props>
    		</property>
    	</bean>
    	<bean id="advertTarget" class="com.company.dreams.services.spring.AdvertService">
    		<property name="baseDao"><ref bean="baseDao"/></property>
    	</bean>
    	<bean id="baseDao" class="com.company.dreams.dao.hibernate.BaseDao">
    		<property name="sessionFactory"><ref local="sf"/></property>
    	</bean>
    BaseDao extends HibernateDaoSupport.

    I ran a junit test and everything worked just fine, I followed this tutorial : java.net: Unit Testing Hibernate Mapping Configurations
    Everything worked just fine. I was able to get my advert Owner.
    When I switched to real application, the advert.getOwner returns null.
    I turned hibernate sql to true and the Service doesn't seem to "touch" the object because there is no sql generated while sql is generated on other methods like save or update.

    This is really strange for me, any help would be appreciated.

    Thanks, Louis.

  • #2
    I guess it would be useful to see the TestCase that works and the service code that doesn't.

    Comment


    • #3
      test cases (on works, the other doesn't)

      Test that passes fine (without any spring configuration file)
      Code:
      package com.company.dreams.dao;
      
      import java.util.ResourceBundle;
      
      import junit.framework.TestCase;
      
      // imports omitted
      
      /**
       * @author littleloulou
       * 
       */
      public class TestAdvertDao extends TestCase {
      
      	Logger logger;
      
      	SessionFactory sessionFactory;
      
      	Session session;
      	
      	ResourceBundle config;
      	
      	BaseDao baseDao;
      	
      	protected void setUp() throws Exception {
      		
      		config = ResourceBundle.getBundle("config");
      	    Configuration configuration = 
      	        new Configuration();
      	    configuration.setProperty(
      	        Environment.DRIVER, 
      	        config.getString("hibernate.connection.driver_class"));
      	    configuration.setProperty(
      	        Environment.URL, 
      	        config.getString("hibernate.connection.url"));
      	    configuration.setProperty(
      	        Environment.USER, 
      	        config.getString("hibernate.connection.userid"));
      	    configuration.setProperty(
      		        Environment.PASS, 
      		        config.getString("hibernate.connection.password"));
      	    configuration.setProperty(
      	        Environment.DIALECT, 
      	        MySQLInnoDBDialect.class.getName());
      	    configuration.setProperty(
      	        Environment.SHOW_SQL, "true");
      	    
      	    configuration.addClass(Users.class);
      	    configuration.addClass(Transaction.class);
      	    configuration.addClass(Advert.class);
      	    configuration.addClass(Housing.class);
                 // other mapping required classes omitted
      
      	    this.sessionFactory = 
      	        configuration.buildSessionFactory();
      
      	    HibernateTemplate hibernateTemplate =
      	        new HibernateTemplate(sessionFactory);
      	    
      	    baseDao = new BaseDao();
      	    baseDao.setHibernateTemplate(hibernateTemplate);
      
      	    this.session = SessionFactoryUtils.
      	        getSession(sessionFactory, true);
      	    TransactionSynchronizationManager.
      	        bindResource(sessionFactory, 
      	            new SessionHolder(session));
      	    
      		logger = Logger.getLogger(TestAdvertDao.class);
      	}
      
      	protected void tearDown() throws Exception {
      	    TransactionSynchronizationManager.
      	        unbindResource(sessionFactory);
      	    SessionFactoryUtils.
      	        releaseSession(session, sessionFactory);
      	}	
      
      	public void testLoad() {
      		try {
      			Housing advert = (Housing) baseDao.load(Housing.class,
      					new Integer(20));
      			assertNotNull(advert.getReference());
      			Users user = advert.getOwner();
      			assertNotNull(user);
      			assertNotNull(user.getEmail());
      			logger.info("login="+user.getEmail());
      			Transaction tx = advert.getCurrentTransaction();
      			assertNotNull(tx);
      			assertNotNull(tx.getAmount());
      			logger.info("amount="+tx.getAmount() + " " + tx.getAdvert().getCurrency().getSign());
      			
      		} catch (Exception e) {
      			logger.error(e);
      			fail("" + e);
      		}
      	}
      
      }
      test that don't pass :

      Code:
      package com.company.dreams.services.spring;
      
      import junit.framework.TestCase;
      
      import org.apache.log4j.Logger;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      import com.company.dreams.domainmodel.Housing;
      import com.company.dreams.domainmodel.Users;
      import com.company.dreams.services.IHousingService;
      
      /**
       * @author littleloulou
       * 
       */
      public class TestHousingService extends TestCase {
      
      	private static IHousingService housingService;
      
      	private ApplicationContext factory;
      
      	private static Logger logger;
      	
      	/**
      	 * @see junit.framework.TestCase#setUp()
      	 */
      	protected void setUp() throws Exception {
      		super.setUp();
      		factory = new ClassPathXmlApplicationContext(
      				new String[] { "test-housingservice.xml" });
      		housingService = (IHousingService) factory
      				.getBean("housingService");
      		logger = Logger.getLogger(TestHousingService.class);
      		
      	}
      
      	public void testFindUser() {
      		try {
      			Housing advert = new Housing();
      			advert.setId(new Integer(20));
      			Users user = (Users)housingService.findUser(advert).get(0);
      			assertNotNull(user);
      			
      		} catch (Exception e) {
      			logger.error(e);
      			fail(""+ e);
      		}
      	}
      	
      }
      
      <?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="propertiesLoader" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
      		<property name="location"><value>config.properties</value></property> 
      	</bean>
      
      	<!-- SOURCE DE DONNEES -->
      	<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      		<property name="driverClassName">
      			<value>${hibernate.connection.driver_class}</value> 
      		</property>
      		<property name="url">
      			<value>${hibernate.connection.url}</value>
      		</property>
      		<property name="username">
      			<value>${hibernate.connection.userid}</value>
      		</property>
      		<property name="password">
      			<value>${hibernate.connection.password}</value>
      		</property>
      	</bean>
      	
      	<bean id="sf" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      		<property name="mappingLocations">
      			<list>
      				<value>classpath:com/company/dreams/domainmodel/Users.hbm.xml</value>
      <value>classpath:com/company/dreams/domainmodel/Transaction.hbm.xml</value>
      <value>classpath:com/company/dreams/domainmodel/Advert.hbm.xml</value>
      <value>classpath:com/company/dreams/domainmodel/Housing.hbm.xml</value>
      <!-- other required mappings omitted -->
      			</list>
      		</property>
      		<property name="hibernateProperties">
      			<props>
      				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      				<prop key="hibernate.show_sql">true</prop>
      				<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
      			</props>
      		</property>
      	
      		<property name="dataSource">
      			<ref bean="ds"/>
      		</property>
      	</bean>
      
      	<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
      	<bean id="tm" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      		<property name="sessionFactory"><ref local="sf"/></property>
      	</bean>
      	
      	<!-- Housing Service -->	
      	<bean id="housingService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">	
      		<property name="transactionManager"><ref local="tm"/></property>
      		<property name="target"><ref local="housingTarget"/></property>
      		<property name="transactionAttributes">
      			<props>
      	 			<prop key="save*">PROPAGATION_REQUIRED,-com.company.dreams.services.ServiceException</prop>
      				<prop key="update*">PROPAGATION_REQUIRED,-com.company.dreams.services.ServiceException</prop>
      				<prop key="delete*">PROPAGATION_REQUIRED,-com.company.dreams.services.ServiceException</prop>
      				<prop key="find*">PROPAGATION_REQUIRED</prop>
      			</props>
      		</property>
      	</bean>
      
      	<bean id="housingTarget" class="com.company.dreams.services.spring.HousingService">
      		<property name="baseDao"><ref local="baseDao"/></property>
      	</bean>
      	<bean id="baseDao" class="com.company.dreams.dao.hibernate.BaseDao">
      		<property name="sessionFactory"><ref local="sf"/></property>
      	</bean>
      </beans>

      Comment


      • #4
        logs for the test case that passes (I had to split the posts)

        logs for the test that passes :
        Code:
        13:21:49,272 [main] DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils  - Opening Hibernate Session 
        13:21:49,379 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Bound value [org.springframework.orm.hibernate3.SessionHolder@19abd2b] for key [org.hibernate.impl.SessionFactoryImpl@56b93a] to thread [main] 
        13:21:49,392 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@19abd2b] for key [org.hibernate.impl.SessionFactoryImpl@56b93a] bound to thread [main] 
        13:21:49,393 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@19abd2b] for key [org.hibernate.impl.SessionFactoryImpl@56b93a] bound to thread [main] 
        13:21:49,394 [main] DEBUG org.springframework.orm.hibernate3.HibernateTemplate  - Found thread-bound Session for HibernateTemplate 
        Hibernate: select housing0_.advert_id as ID5_0_, housing0_1_.PRICE as PRICE5_0_, housing0_1_.REFERENCE as REFERENCE5_0_, housing0_1_.PHONE1 as PHONE4_5_0_, housing0_1_.PHONING1_PERIOD as PHONING5_5_0_, housing0_1_.PHONE2 as PHONE6_5_0_, housing0_1_.PHONING2_PERIOD as PHONING7_5_0_, housing0_1_.EMAIL as EMAIL5_0_, housing0_1_.WEB_SITE as WEB9_5_0_, housing0_1_.CITY as CITY5_0_, housing0_1_.ZIPCODE as ZIPCODE5_0_, housing0_1_.NEAR_CITY as NEAR12_5_0_, housing0_1_.NEAR_DISTANCE as NEAR13_5_0_, housing0_1_.CURRENCY_ID as CURRENCY14_5_0_, housing0_1_.COUNTRY_CODE as COUNTRY15_5_0_, housing0_1_.DIALING2_CODE as DIALING16_5_0_, housing0_1_.DIALING1_CODE as DIALING17_5_0_, housing0_1_.CATEGORY_CODE as CATEGORY18_5_0_, housing0_.QUARTER as QUARTER21_0_, housing0_.ALTITUDE as ALTITUDE21_0_, housing0_.SKI_RESORT as SKI4_21_0_, housing0_.SKI_RESORT_DISTANCE as SKI5_21_0_, housing0_.NUMBER_ROOM_VALUE as NUMBER6_21_0_, housing0_.HABITABLE_AREA as HABITABLE7_21_0_, housing0_.GROUND_AREA as GROUND8_21_0_, housing0_.IS_AVAILABLE as IS9_21_0_, housing0_.DISPONIBILITY_DATE as DISPONI10_21_0_, housing0_.IS_APPROVED as IS11_21_0_, housing0_.FACADE_LENGTH as FACADE12_21_0_, housing0_.IS_PLANTED as IS13_21_0_, housing0_.COS as COS21_0_, housing0_.SHON as SHON21_0_, housing0_.IS_OUTBUILDINGS as IS16_21_0_, housing0_.OUTBUILDINGS_AREA as OUTBUIL17_21_0_, housing0_.IS_CONVERTED_ROOF as IS18_21_0_, housing0_.CONVERTED_ROOF_AREA as CONVERTED19_21_0_, housing0_.IS_SWIMMING_POOL as IS20_21_0_, housing0_.SWIMMING_POOL_LENGTH as SWIMMING21_21_0_, housing0_.SWIMMING_POOL_WIDTH as SWIMMING22_21_0_, housing0_.SWIMMING_POOL_DIAMETER as SWIMMING23_21_0_, housing0_.IS_TENNIS_COURT as IS24_21_0_, housing0_.IS_INDOOR_TENNIS_COURT as IS25_21_0_, housing0_.CONSTRUCTION_YEAR as CONSTRU26_21_0_, housing0_.FLOOR_LEVEL_VALUE as FLOOR27_21_0_, housing0_.IS_LIFTED as IS28_21_0_, housing0_.NUMBER_FLOOR as NUMBER29_21_0_, housing0_.CEILING_HEIGHT as CEILING30_21_0_, housing0_.IS_PRIVATE_HEATED as IS31_21_0_, housing0_.IS_CO_OWNERSHIPED as IS32_21_0_, housing0_.SIMPLE_LIVING_ROOM as SIMPLE33_21_0_, housing0_.SIMPLE_LIVING_ROOM_CHIMNEY as SIMPLE34_21_0_, housing0_.SIMPLE_LIVING_ROOM_AREA as SIMPLE35_21_0_, housing0_.DOUBLE_LIVING_ROOM as DOUBLE36_21_0_, housing0_.DOUBLE_LIVING_ROOM_CHIMNEY as DOUBLE37_21_0_, housing0_.DOUBLE_LIVING_ROOM_AREA as DOUBLE38_21_0_, housing0_.KITCHEN as KITCHEN21_0_, housing0_.FITTED_KITCHEN as FITTED40_21_0_, housing0_.AMERICAN_KITCHEN as AMERICAN41_21_0_, housing0_.AMERICAN_FITTED_KITCHEN as AMERICAN42_21_0_, housing0_.BEDROOM as BEDROOM21_0_, housing0_.FLOOR_BEDROOM as FLOOR44_21_0_, housing0_.BATHROOM as BATHROOM21_0_, housing0_.BATHROOM_WITH_WC as BATHROOM46_21_0_, housing0_.SHOWER_ROOM as SHOWER47_21_0_, housing0_.SHOWER_ROOM_WITH_WC as SHOWER48_21_0_, housing0_.WC as WC21_0_, housing0_.STUDY_ROOM as STUDY50_21_0_, housing0_.DRESSING_ROOM as DRESSING51_21_0_, housing0_.LAUNDRY_ROOM as LAUNDRY52_21_0_, housing0_.BALCONY as BALCONY21_0_, housing0_.BALCONY_AREA as BALCONY54_21_0_, housing0_.TERRACE as TERRACE21_0_, housing0_.TERRACE_AREA as TERRACE56_21_0_, housing0_.VERANDA as VERANDA21_0_, housing0_.VERANDA_AREA as VERANDA58_21_0_, housing0_.INSIDE_GARAGE as INSIDE59_21_0_, housing0_.IS_BASEMENT_GARAGE as IS60_21_0_, housing0_.INSIDE_PARKING as INSIDE61_21_0_, housing0_.IS_BASEMENT_PARKING as IS62_21_0_, housing0_.OUTSIDE_GARAGE as OUTSIDE63_21_0_, housing0_.OUTSIDE_PARKING as OUTSIDE64_21_0_, housing0_.IS_BASEMENT as IS65_21_0_, housing0_.IS_CELLAR as IS66_21_0_, housing0_.IS_SKI_LOCKER as IS67_21_0_, housing0_.ZONE_ID as ZONE68_21_0_, housing0_.CONSTRUCTION_ID as CONSTRU69_21_0_, housing0_.FLOOR_LEVEL_ID as FLOOR70_21_0_, housing0_.MEASURE_UNIT_ID as MEASURE71_21_0_, housing0_.EXPOSURE1_ID as EXPOSURE72_21_0_, housing0_.EXPOSURE2_ID as EXPOSURE73_21_0_, housing0_.CONDITIONS_ID as CONDITIONS74_21_0_, housing0_.NEAR_PISTE_ID as NEAR75_21_0_, housing0_.NUMBER_ROOM_ID as NUMBER76_21_0_ from HOUSING housing0_ inner join ADVERT housing0_1_ on housing0_.advert_id=housing0_1_.ID where housing0_.advert_id=?
        Hibernate: select currency0_.ID as ID11_0_, currency0_.RANK as RANK11_0_, currency0_.SIGN as SIGN11_0_, currency0_.RATE as RATE11_0_, currency0_.RATE_DATE as RATE5_11_0_ from CURRENCY currency0_ where currency0_.ID=?
        Hibernate: select country0_.CODE as CODE13_0_, country0_.DIALING_CODE as DIALING2_13_0_ from COUNTRY country0_ where country0_.CODE=?
        Hibernate: select category0_.CODE as CODE9_0_, category0_.RANK as RANK9_0_, category0_.IS_COMPLEX as IS3_9_0_, category0_.PARENT as PARENT9_0_ from CATEGORY category0_ where category0_.CODE=?
        Hibernate: select category0_.CODE as CODE9_0_, category0_.RANK as RANK9_0_, category0_.IS_COMPLEX as IS3_9_0_, category0_.PARENT as PARENT9_0_ from CATEGORY category0_ where category0_.CODE=?
        13:21:49,721 [main] DEBUG org.springframework.orm.hibernate3.HibernateTemplate  - Not closing pre-bound Hibernate Session after HibernateTemplate 
        Hibernate: select transactio0_.ADVERT_ID as ADVERT2_1_, transactio0_.USERS_ID as USERS1_1_, transactio0_.START_DATE as START3_1_, transactio0_.USERS_ID as USERS1_2_0_, transactio0_.ADVERT_ID as ADVERT2_2_0_, transactio0_.START_DATE as START3_2_0_, transactio0_.FINISH_DATE as FINISH4_2_0_, transactio0_.PAID as PAID2_0_, transactio0_.PUBLISH_DATE as PUBLISH6_2_0_, transactio0_.END_DATE as END7_2_0_, transactio0_.DELETE_DATE as DELETE8_2_0_, transactio0_.PAYMENT_SUPPORT_NAME as PAYMENT9_2_0_, transactio0_.PAYMENT_SUPPORT_NUMBER as PAYMENT10_2_0_, transactio0_.AMOUNT as AMOUNT2_0_, transactio0_.PAYMENT_TYPE_CODE as PAYMENT12_2_0_ from TRANSACTION transactio0_ where transactio0_.ADVERT_ID=?
        Hibernate: select users0_.ID as ID0_0_, users0_.IS_CUSTOMER as IS2_0_0_, users0_.PWD as PWD0_0_, users0_.COMPANY as COMPANY0_0_, users0_.NAME as NAME0_0_, users0_.SURNAME as SURNAME0_0_, users0_.EMAIL as EMAIL0_0_, users0_.ADDRESS as ADDRESS0_0_, users0_.ZIPCODE as ZIPCODE0_0_, users0_.CITY as CITY0_0_, users0_.PHONE as PHONE0_0_, users0_.FAX as FAX0_0_, users0_.CREATED_DATE as CREATED13_0_0_, users0_.LAST_CONNECTED_DATE as LAST14_0_0_, users0_.TRIAL_NUMBER as TRIAL15_0_0_, users0_.IS_PRIVATE_DATA_PUBLISHED as IS16_0_0_, users0_.IS_DELETED as IS17_0_0_, users0_.AGE_ID as AGE18_0_0_, users0_.DIALING_PHONE_CODE as DIALING19_0_0_, users0_.COUNTRY_CODE as COUNTRY20_0_0_, users0_.DIALING_FAX_CODE as DIALING21_0_0_, users0_.LANGUAGE_ID as LANGUAGE22_0_0_, users0_.TITLE_ID as TITLE23_0_0_, users0_.CATEGORY_CODE as CATEGORY24_0_0_ from USERS users0_ where users0_.ID=?
        Hibernate: select age0_.ID as ID17_0_, age0_.RANK as RANK17_0_ from AGE age0_ where age0_.ID=?
        Hibernate: select language0_.ID as ID8_0_, language0_.LABEL as LABEL8_0_ from LANGUAGE language0_ where language0_.ID=?
        Hibernate: select title0_.ID as ID19_0_ from TITLE title0_ where title0_.ID=?
        13:21:49,870 [main] INFO  com.company.dreams.dao.TestAdvertDao  - login=[email protected] 
        13:21:49,871 [main] INFO  com.company.dreams.dao.TestAdvertDao  - amount=25.0  
        13:21:49,872 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Removed value [org.springframework.orm.hibernate3.SessionHolder@19abd2b] for key [org.hibernate.impl.SessionFactoryImpl@56b93a] from thread [main] 
        13:21:49,872 [main] DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils  - Closing Hibernate Session

        Comment


        • #5
          logs for the test case that didn't pass (I had to split the posts)

          logs for the test that didn't pass :
          Code:
          13:15:11,251 [main] INFO  org.springframework.aop.framework.DefaultAopProxyFactory  - CGLIB2 available: proxyTargetClass feature enabled 
          13:15:11,380 [main] DEBUG org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource  - Adding transactional method [delete*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-com.company.dreams.services.ServiceException] 
          13:15:11,381 [main] DEBUG org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource  - Adding transactional method [find*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT] 
          13:15:11,382 [main] DEBUG org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource  - Adding transactional method [update*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-com.company.dreams.services.ServiceException] 
          13:15:11,383 [main] DEBUG org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource  - Adding transactional method [save*] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT,-com.company.dreams.services.ServiceException] 
          13:15:11,418 [main] DEBUG org.springframework.aop.framework.ProxyFactory  - Added new aspect interface: com.company.dreams.services.IHousingService 
          13:15:11,425 [main] DEBUG org.springframework.aop.framework.JdkDynamicAopProxy  - Creating JDK dynamic proxy for [com.company.dreams.services.spring.HousingService] 
          13:15:11,493 [main] INFO  org.springframework.jdbc.datasource.JdbcTransactionObjectSupport  - JDBC 3.0 Savepoint class is available 
          13:15:11,520 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@1531164] 
          13:15:11,521 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Creating new transaction with name [com.company.dreams.services.IHousingService.findUser] 
          13:15:11,626 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Opened new Session [org.hibernate.impl.SessionImpl@15fc672] for Hibernate transaction 
          13:15:11,637 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@15fc672] 
          13:15:11,670 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Exposing Hibernate transaction as JDBC transaction [org.apache.commons.dbcp.PoolableConnection@178b64b] 
          13:15:11,672 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@83020] for key [org.apache.commons.dbcp.BasicDataSource@c59ad5] to thread [main] 
          13:15:11,672 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Bound value [org.springframework.orm.hibernate3.SessionHolder@127461b] for key [org.hibernate.impl.SessionFactoryImpl@16b9e62] to thread [main] 
          13:15:11,673 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Initializing transaction synchronization 
          13:15:11,676 [main] DEBUG org.springframework.transaction.interceptor.TransactionInterceptor  - Getting transaction for com.company.dreams.services.IHousingService.findUser 
          13:15:11,677 [main] DEBUG org.springframework.transaction.interceptor.TransactionInterceptor  - Invoking commit for transaction on com.company.dreams.services.IHousingService.findUser 
          13:15:11,680 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Triggering beforeCommit synchronization 
          13:15:11,694 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Triggering beforeCompletion synchronization 
          13:15:11,695 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Initiating transaction commit 
          13:15:11,696 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@15fc672] 
          13:15:11,698 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Triggering afterCommit synchronization 
          13:15:11,699 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Triggering afterCompletion synchronization 
          13:15:11,700 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Clearing transaction synchronization 
          13:15:11,700 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Removed value [org.springframework.orm.hibernate3.SessionHolder@127461b] for key [org.hibernate.impl.SessionFactoryImpl@16b9e62] from thread [main] 
          13:15:11,700 [main] DEBUG org.springframework.transaction.support.TransactionSynchronizationManager  - Removed value [org.springframework.jdbc.datasource.ConnectionHolder@83020] for key [org.apache.commons.dbcp.BasicDataSource@c59ad5] from thread [main] 
          13:15:11,701 [main] DEBUG org.springframework.orm.hibernate3.HibernateTransactionManager  - Closing Hibernate Session [org.hibernate.impl.SessionImpl@15fc672] after transaction 
          13:15:11,702 [main] DEBUG org.springframework.orm.hibernate3.SessionFactoryUtils  - Closing Hibernate Session 
          Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 9.639 sec <<< FAILURE!

          Comment


          • #6
            It is really hard to see what is going on here. I would be useful if the code and the configuration are kept in different blocks. Otherwise, it's really hard to read. As for the problem, I think probably the two most important classes aren't here; namely HousingService and BaseDao. Any chance I could get a look at them?

            Comment


            • #7
              sure u can have a look at them

              Thanks a lot for your reply here is HousingService :

              Code:
              package com.company.dreams.services.spring;
              //imports omitted
              /**
               * @author littleloulou
               *
               */
              public class HousingService implements IHousingService {
              	private IBaseDao baseDao;
              
                      // other methods omitted
              
              	/**
              	 * @param baseDao The baseDao to set.
              	 */
              	public void setBaseDao(IBaseDao baseDao) {
              		this.baseDao = baseDao;
              	}
              
              	public List findUser(Housing housing) throws ServiceException {
              		if (housing == null || housing.getId() == null) throw new IllegalArgumentException("housing ou housing.id est null");
              		List l = new ArrayList();
              		try {
              			l.add(housing.getOwner());
              		} catch (Exception e) {
              			logger.error(e);
              			throw new ServiceException(e.getMessage());
              		}
              		return l;
              	}
              }
              Here is IHousingService :
              Code:
              package com.company.dreams.services;
              import java.util.List;
              import com.company.dreams.domainmodel.Housing;
              /**
               * @author littleloulou
               *
               */
              public interface IHousingService {
              	Housing save(Housing housing) throws ServiceException;
              	List findUser(Housing housing) throws ServiceException;
              }
              Here is BaseDao :
              Code:
              package com.company.dreams.dao.hibernate;
              // imports omitted
              
              public class BaseDao extends HibernateDaoSupport implements IBaseDao {
                  
                  public Object load(Class clazz, Serializable id){
                  	return getHibernateTemplate().get(clazz, id);
                  }
                  
                  public void update(Object obj){
                  	try {
                  		//getSession().setFlushMode(FlushMode.AUTO);
                  		getHibernateTemplate().saveOrUpdate(obj);
                  	} catch (Exception e) {
                  		Logger.getLogger(BaseDao.class).error("BaseDao.update="+e);
                  		e.printStackTrace();
                  	}
              	}
              	
                  public void delete(Object obj){
                      getHibernateTemplate().delete(obj);
              	}
              	
                  public Object save(Object obj) {
                  	try {
                  		//getSession().setFlushMode(FlushMode.AUTO);
                  		getHibernateTemplate().save(obj);
                  	} catch (Exception e) {
                  		e.printStackTrace();
                  		Logger.getLogger(BaseDao.class).error("BaseDao.save="+e);
                  	}
                      return obj;
              	}
              
                  public Object saveOrUpdate(Object obj) {
                  	getHibernateTemplate().saveOrUpdate(obj);
                      return obj;
              	}
                  
                  
                  public List findAll(Class clazz) {
                  	return getHibernateTemplate().loadAll(clazz);
                  }
              
              	public List findByLanguage(Object obj, Locale l) throws DaoException {
                      try {
                      	String hql = "from " + obj.getClass().getName() + " obj where obj.language.id='" + l.getLanguage() + "' order by obj.label";
                          return getSession().createQuery(hql).list();
                      } catch (Exception e) {
              	        Logger.getLogger(this.getClass().getName()).error(".findByLanguage, " + obj.getClass().getName() + " " + e);
              		    throw new DaoException(this.getClass().getName() + ".findByLanguage, " + obj.getClass().getName() + " " + e.getMessage());
                      }
              	}
              	
              }
              Here is IBaseDao :
              Code:
              package com.company.dreams.dao;
              
              import java.io.Serializable;
              import java.util.List;
              
              public interface IBaseDao {
              
                  public Object load(Class clazz, Serializable id);
              
              	public void update(Object obj);
              	
              	public void delete(Object obj);
              	
              	public Object save(Object obj);
              	
              	public Object saveOrUpdate(Object obj);
              	
              	public List findAll(Class clazz);	
              }
              Hope this helps ...

              Comment


              • #8
                I don't see why the code would work! HousingService simply returns a List containing the passed in housing.getOwner(). In the test that doesn't work, you are creating a Housing and then calling findUser(..). This is simply going to return that user.getOwner(). In the test that does work, you are actually looking up the item from the database. I would assume you want to be doing the same thing in both places.

                Test that doesn't work
                Code:
                Housing advert = new Housing();
                advert.setId(new Integer(20));			
                Users user = (Users)housingService.findUser(advert).get(0);
                HousingService
                Code:
                	public List findUser(Housing housing) throws ServiceException {
                		if (housing == null || housing.getId() == null) throw new IllegalArgumentException("housing ou housing.id est null");
                		List l = new ArrayList();
                		try {
                			l.add(housing.getOwner());
                		} catch (Exception e) {
                			logger.error(e);
                			throw new ServiceException(e.getMessage());
                		}
                		return l;
                	}
                BaseDao
                Code:
                    public Object load(Class clazz, Serializable id){
                    	return getHibernateTemplate().get(clazz, id);
                    }

                Comment


                • #9
                  Thank you much Karl

                  I'd like to thank you karl because you solved my problem.
                  The objcet I was testing was obviously detached from the hibernate session.
                  I needed to re-attach it by loading it from database.
                  The same happened in the real use case. I don't know were I had my head last 7 days.
                  The bug was obvious. Your external look saw it in one day.
                  I was thinking of some "magic" that would re-attach it automatically just by touching it lol.
                  My both test pass and the so does the use case.

                  Anyway thank you for being so patient ...

                  Comment


                  • #10
                    Not a problem, I'm glad to help. Hibernate is good, but it's not quite that good. Great that you've got it sorted, sometimes it only needs a second pair of eyes. Good luck with the rest of it!

                    Comment

                    Working...
                    X