Announcement Announcement Module
Collapse
No announcement yet.
CommitException ?? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • CommitException ??

    Hi everyone,

    I am using hibernate v2.1.8 (with MySQL v4.1) and Spring v1.2.3.

    I have created a Parent-Child relationship, as Country > State > Province and so on... with lazy fetching enabled (that's the intention)

    I am not able to understand, but the transaction is rolling back. Can somebody help me understand, why such a thing is happening...

    The code files are listed below... Please help !!

    Thanks in advance,
    vaibhav

    The Console Output
    Code:
    Execute:
         [java] Starting Geographical Service Client
         [java] log4j:WARN No appenders could be found for logger (org.springframewo
    rk.beans.factory.xml.XmlBeanDefinitionReader).
         [java] log4j:WARN Please initialize the log4j system properly.
         [java] Application Context Loaded :-)
         [java] Hibernate: insert into COUNTRY (label) values (?)
         [java] Country ID = 1
         [java] Hibernate: select country0_.COUNTRY_ID as COUNTRY_ID0_, country0_.la
    bel as label0_ from COUNTRY country0_ where country0_.COUNTRY_ID=?
         [java] Label: ABC
     [java] Trying to add a State
         [java] DEBUG - TransactionAspectSupport.createTransactionIfNecessary(213) | Getting transaction for com.abc.service.TerritorialDataService.addState
         [java] DEBUG - AbstractPlatformTransactionManager.getTransaction(252) | Using transaction object [org.springframework.orm.hibernate.HibernateTransactionManager$HibernateTransactionObject@e576d4]
         [java] DEBUG - AbstractPlatformTransactionManager.getTransaction(279) | Creating new transaction with name [com.abc.service.TerritorialDataService.addState]
         &#91;java&#93; DEBUG - SessionImpl.<init>&#40;560&#41; | opened session
         &#91;java&#93; DEBUG - HibernateTransactionManager.doBegin&#40;400&#41; | Opened new Session &#91;net.sf.hibernate.impl.SessionImpl@dc86eb&#93; for Hibernate transaction
         &#91;java&#93; DEBUG - DriverManagerDataSource.getConnectionFromDriverManager&#40;288&#41; | Creating new JDBC Connection to &#91;jdbc&#58;mysql&#58;//localhost&#58;3306/cdcDB&#93;
         &#91;java&#93; DEBUG - JDBCTransaction.begin&#40;37&#41; | begin
         &#91;java&#93; DEBUG - JDBCTransaction.begin&#40;41&#41; | current autocommit status&#58;true
         &#91;java&#93; DEBUG - JDBCTransaction.begin&#40;43&#41; | disabling autocommit
         &#91;java&#93; DEBUG - HibernateTransactionManager.doBegin&#40;441&#41; | Exposing Hibernate transaction as JDBC transaction &#91;com.mysql.jdbc.Connection@cbf9bd&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.bindResource&#40;161&#41; | Bound value &#91;org.springframework.jdbc.datasource.ConnectionHolder@17918f0&#93; for key &#91;[email protected]c&#93; to thread &#91;main&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.bindResource&#40;161&#41; | Bound value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; to thread &#91;main&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.initSynchronization&#40;213&#41; | Initializing transaction synchronization
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.getResource&#40;136&#41; | Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; bound to thread &#91;main&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.getResource&#40;136&#41; | Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; bound to thread &#91;main&#93;
         &#91;java&#93; DEBUG - HibernateTemplate.execute&#40;310&#41; | Found thread-bound Session for HibernateTemplate
         &#91;java&#93; DEBUG - SessionImpl.doLoadByClass&#40;1996&#41; | loading &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.doLoad&#40;2094&#41; | attempting to resolve &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.doLoad&#40;2130&#41; | object not resolved in any cache &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - EntityPersister.load&#40;410&#41; | Materializing entity&#58; &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - BatcherImpl.logOpenPreparedStatement&#40;204&#41; | about to open&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.log&#40;230&#41; | select country0_.COUNTRY_ID as COUNTRY_ID0_, country0_.label as label0_ from COUNTRY country0_ where country0_.COUNTRY_ID=?
         &#91;java&#93; Hibernate&#58; select country0_.COUNTRY_ID as COUNTRY_ID0_, country0_.label as label0_ from COUNTRY country0_ where country0_.COUNTRY_ID=?
         &#91;java&#93; DEBUG - BatcherImpl.getPreparedStatement&#40;253&#41; | preparing statement
         &#91;java&#93; DEBUG - NullableType.nullSafeSet&#40;46&#41; | binding '1' to parameter&#58; 1
         &#91;java&#93; DEBUG - Loader.doQuery&#40;281&#41; | processing result set
         &#91;java&#93; DEBUG - Loader.getRow&#40;484&#41; | result row&#58; 1
         &#91;java&#93; DEBUG - Loader.loadFromResultSet&#40;615&#41; | Initializing object from ResultSet&#58; 1
         &#91;java&#93; DEBUG - Loader.hydrate&#40;684&#41; | Hydrating entity&#58; com.abc.persistence.Country#1
         &#91;java&#93; DEBUG - NullableType.nullSafeGet&#40;68&#41; | returning 'ABC' as column&#58; label0_
         &#91;java&#93; DEBUG - Loader.doQuery&#40;298&#41; | done processing result set &#40;1 rows&#41;
         &#91;java&#93; DEBUG - BatcherImpl.logClosePreparedStatement&#40;211&#41; | done closing&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.closePreparedStatement&#40;275&#41; | closing statement
         &#91;java&#93; DEBUG - Loader.initializeEntitiesAndCollections&#40;318&#41; | total objects hydrated&#58; 1
         &#91;java&#93; DEBUG - SessionImpl.initializeEntity&#40;2216&#41; | resolving associations for &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.getCollection&#40;3994&#41; | creating collection wrapper&#58;&#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.initializeEntity&#40;2247&#41; | done materializing entity &#91;com.abc.persistence.Country#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.initializeNonLazyCollections&#40;3161&#41; | initializing non-lazy collections
         &#91;java&#93; DEBUG - HibernateTemplate.execute&#40;333&#41; | Not closing pre-bound Hibernate Session after HibernateTemplate
         &#91;java&#93; Country Id = 1
         &#91;java&#93; DEBUG - SessionImpl.initializeCollection&#40;3307&#41; | initializing collection &#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.initializeCollection&#40;3308&#41; | checking second-level cache
         &#91;java&#93; DEBUG - SessionImpl.initializeCollection&#40;3314&#41; | collection not cached
         &#91;java&#93; DEBUG - BatcherImpl.logOpenPreparedStatement&#40;204&#41; | about to open&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.log&#40;230&#41; | select states0_.COUNTRY_ID as COUNTRY_ID__, states0_.STATE_ID as STATE_ID__, states0_.STATE_ID as STATE_ID0_, states0_.label as label0_, states0_.COUNTRY_ID as COUNTRY_ID0_ from STATE states0_ where states0_.COUNTRY_ID=?
         &#91;java&#93; Hibernate&#58; select states0_.COUNTRY_ID as COUNTRY_ID__, states0_.STATE_ID as STATE_ID__, states0_.STATE_ID as STATE_ID0_, states0_.label as label0_, states0_.COUNTRY_ID as COUNTRY_ID0_ from STATE states0_ where states0_.COUNTRY_ID=?
         &#91;java&#93; DEBUG - BatcherImpl.getPreparedStatement&#40;253&#41; | preparing statement
         &#91;java&#93; DEBUG - NullableType.nullSafeSet&#40;46&#41; | binding '1' to parameter&#58; 1
         &#91;java&#93; DEBUG - Loader.handleEmptyCollections&#40;406&#41; | result set contains &#40;possibly empty&#41; collection&#58; &#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.getLoadingCollection&#40;3050&#41; | uninitialized collection&#58; initializing
         &#91;java&#93; DEBUG - Loader.doQuery&#40;281&#41; | processing result set
         &#91;java&#93; DEBUG - Loader.doQuery&#40;298&#41; | done processing result set &#40;0 rows&#41;
         &#91;java&#93; DEBUG - BatcherImpl.logClosePreparedStatement&#40;211&#41; | done closing&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.closePreparedStatement&#40;275&#41; | closing statement
         &#91;java&#93; DEBUG - Loader.initializeEntitiesAndCollections&#40;318&#41; | total objects hydrated&#58; 0
         &#91;java&#93; DEBUG - SessionImpl.endLoadingCollections&#40;3109&#41; | 1 collections were found in result set
         &#91;java&#93; DEBUG - SessionImpl.endLoadingCollections&#40;3140&#41; | collection fully initialized&#58; &#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.endLoadingCollections&#40;3143&#41; | 1 collections initialized
         &#91;java&#93; DEBUG - SessionImpl.initializeNonLazyCollections&#40;3161&#41; | initializing non-lazy collections
         &#91;java&#93; DEBUG - SessionImpl.initializeCollection&#40;3316&#41; | collection initialized
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.getResource&#40;136&#41; | Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; bound to thread &#91;main&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.getResource&#40;136&#41; | Retrieved value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; bound to thread &#91;main&#93;
         &#91;java&#93; DEBUG - HibernateTemplate.execute&#40;310&#41; | Found thread-bound Session for HibernateTemplate
         &#91;java&#93; DEBUG - SessionImpl.update&#40;1351&#41; | object already associated with session
         &#91;java&#93; DEBUG - HibernateTemplate.execute&#40;333&#41; | Not closing pre-bound Hibernate Session after HibernateTemplate
         &#91;java&#93; DEBUG -
    TransactionAspectSupport.doCommitTransactionAfterR eturning(256)
    Code:
     | Invoking commit for transaction on com.abc.service.TerritorialDataService.addState
         &#91;java&#93; DEBUG - AbstractPlatformTransactionManager.triggerBeforeCommit&#40;652&#41; | Triggering beforeCommit synchronization
         &#91;java&#93; DEBUG - AbstractPlatformTransactionManager.triggerBeforeCompletion&#40;668&#41; | Triggering beforeCompletion synchronization
         &#91;java&#93; DEBUG - AbstractPlatformTransactionManager.processCommit&#40;492&#41; | Initiating transaction commit
         &#91;java&#93; DEBUG - HibernateTransactionManager.doCommit&#40;487&#41; | Committing Hibernate transaction on Session &#91;net.sf.hibernate.impl.SessionImpl@dc86eb&#93;
         &#91;java&#93; DEBUG - JDBCTransaction.commit&#40;59&#41; | commit
         &#91;java&#93; DEBUG - SessionImpl.flushEverything&#40;2267&#41; | flushing session
         &#91;java&#93; DEBUG - Cascades.cascade&#40;497&#41; | processing cascades for&#58; com.abc.persistence.Country
         &#91;java&#93; DEBUG - Cascades.cascadeCollection&#40;524&#41; | cascading to collection&#58; com.abc.persistence.Country.states
         &#91;java&#93; DEBUG - Cascades$4.cascade&#40;113&#41; | cascading to saveOrUpdate&#40;&#41;
         &#91;java&#93; DEBUG - SessionImpl.saveOrUpdate&#40;1397&#41; | saveOrUpdate&#40;&#41; unsaved instance
         &#91;java&#93; DEBUG - SessionImpl.doSave&#40;836&#41; | saving &#91;com.abc.persistence.State#<null>&#93;
         &#91;java&#93; DEBUG - SessionImpl.executeInserts&#40;2330&#41; | executing insertions
         &#91;java&#93; DEBUG - Cascades.cascade&#40;497&#41; | processing cascades for&#58; com.abc.persistence.State
         &#91;java&#93; DEBUG - Cascades.cascade&#40;506&#41; | done processing cascades for&#58; com.abc.persistence.State
         &#91;java&#93; DEBUG - WrapVisitor.processArrayOrNewCollection&#40;81&#41; | Wrapped collection in role&#58; com.abc.persistence.State.districts
         &#91;java&#93; DEBUG - EntityPersister.insert&#40;484&#41; | Inserting entity&#58; com.abc.persistence.State &#40;native id&#41;
         &#91;java&#93; DEBUG - BatcherImpl.logOpenPreparedStatement&#40;204&#41; | about to open&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.log&#40;230&#41; | insert into STATE &#40;label, COUNTRY_ID&#41; values &#40;?, ?&#41;
         &#91;java&#93; Hibernate&#58; insert into STATE &#40;label, COUNTRY_ID&#41; values &#40;?, ?&#41;
         &#91;java&#93; DEBUG - BatcherImpl.getPreparedStatement&#40;253&#41; | preparing statement
         &#91;java&#93; DEBUG - EntityPersister.dehydrate&#40;382&#41; | Dehydrating entity&#58; &#91;com.abc.persistence.State#<null>&#93;
         &#91;java&#93; DEBUG - NullableType.nullSafeSet&#40;46&#41; | binding 'Uttar Pradesh' to parameter&#58; 1
         &#91;java&#93; DEBUG - NullableType.nullSafeSet&#40;46&#41; | binding '1' to parameter&#58; 2
         &#91;java&#93; DEBUG - AbstractEntityPersister.getGeneratedIdentity&#40;1234&#41; | Natively generated identity&#58; 1
         &#91;java&#93; DEBUG - BatcherImpl.logClosePreparedStatement&#40;211&#41; | done closing&#58; 0 open PreparedStatements, 0 open ResultSets
         &#91;java&#93; DEBUG - BatcherImpl.closePreparedStatement&#40;275&#41; | closing statement
         &#91;java&#93; DEBUG - Cascades.cascade&#40;497&#41; | processing cascades for&#58; com.abc.persistence.State
         &#91;java&#93; DEBUG - Cascades.cascadeCollection&#40;524&#41; | cascading to collection&#58; com.abc.persistence.State.districts
         &#91;java&#93; DEBUG - Cascades.cascade&#40;506&#41; | done processing cascades for&#58; com.abc.persistence.State
         &#91;java&#93; DEBUG - Cascades.cascade&#40;506&#41; | done processing cascades for&#58; com.abc.persistence.Country
         &#91;java&#93; DEBUG - SessionImpl$CollectionEntry.preFlush&#40;345&#41; | Collection dirty&#58; &#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.flushEntities&#40;2467&#41; | Flushing entities and processing referenced collections
         &#91;java&#93; DEBUG - SessionImpl.updateReachableCollection&#40;2916&#41; | Collection found&#58; &#91;com.abc.persistence.Country.states#1&#93;, was&#58; &#91;com.abc.persistence.Country.states#1&#93;
         &#91;java&#93; DEBUG - SessionImpl.updateReachableCollection&#40;2916&#41; | Collection found&#58; &#91;com.abc.persistence.State.districts#1&#93;, was&#58; &#91;<unreferenced>&#93;
         &#91;java&#93; DEBUG - SessionImpl.flushCollections&#40;2808&#41; | Processing unreferenced collections
         &#91;java&#93; DEBUG - SessionImpl.flushCollections&#40;2822&#41; | Scheduling collection removes/&#40;re&#41;creates/updates
         &#91;java&#93; DEBUG - SessionImpl.finalize&#40;3435&#41; | running Session.finalize&#40;&#41;
         &#91;java&#93; DEBUG - SessionImpl.flushEverything&#40;2291&#41; | Flushed&#58; 0 insertions, 0 updates, 0 deletions to 2 objects
         &#91;java&#93; DEBUG - SessionImpl.flushEverything&#40;2296&#41; | Flushed&#58; 1 &#40;re&#41;creations, 1 updates, 0 removals to 2 collections
         &#91;java&#93; DEBUG - Printer.toString&#40;75&#41; | listing entities&#58;
         &#91;java&#93; DEBUG - Printer.toString&#40;82&#41; | com.abc.persistence.State&#123;country=Country#1, label=Uttar Pradesh, stateID=1, districts=&#91;&#93;&#125;
         &#91;java&#93; DEBUG - Printer.toString&#40;82&#41; | com.abc.persistence.Country&#123;states=&#91;State#1&#93;, countryID=1, label=ABC&#125;
         &#91;java&#93; DEBUG - SessionImpl.execute&#40;2380&#41; | executing flush
         &#91;java&#93; DEBUG - SessionImpl.postFlush&#40;2852&#41; | post flush
         &#91;java&#93; DEBUG - AbstractPlatformTransactionManager.doRollbackOnCommitException&#40;626&#41; | Initiating transaction rollback on commit exception
         &#91;java&#93; java.lang.StackOverflowError
         &#91;java&#93; DEBUG - HibernateTransactionManager.doRollback&#40;506&#41; | Rolling back Hibernate transaction on Session &#91;net.sf.hibernate.impl.SessionImpl@dc86eb&#93;
         &#91;java&#93; DEBUG - JDBCTransaction.rollback&#40;82&#41; | rollback
         &#91;java&#93; DEBUG - SessionImpl.afterTransactionCompletion&#40;596&#41; | transaction completion
         &#91;java&#93; DEBUG - JDBCTransaction.toggleAutoCommit&#40;103&#41; | re-enabling autocommit
         &#91;java&#93; DEBUG - AbstractPlatformTransactionManager.triggerAfterCompletion&#40;692&#41; | Triggering afterCompletion synchronization
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.clearSynchronization&#40;259&#41; | Clearing transaction synchronization
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.unbindResource&#40;184&#41; | Removed value &#91;org.springframework.orm.hibernate.SessionHolder@1546dbc&#93; for key &#91;net.sf.hibernate.impl.SessionFactoryImpl@190a0d6&#93; from thread &#91;main&#93;
         &#91;java&#93; DEBUG - TransactionSynchronizationManager.unbindResource&#40;184&#41; | Removed value &#91;org.springframework.jdbc.datasource.ConnectionHolder@17918f0&#93; for key &#91;[email protected]c&#93; from thread &#91;main&#93;
         &#91;java&#93; DEBUG - HibernateTransactionManager.doCleanupAfterCompletion&#40;561&#41; | Closing Hibernate Session &#91;net.sf.hibernate.impl.SessionImpl@dc86eb&#93; after transaction
         &#91;java&#93; DEBUG - SessionFactoryUtils.doClose&#40;725&#41; | Closing Hibernate Session
         &#91;java&#93; DEBUG - SessionImpl.close&#40;578&#41; | closing session
         &#91;java&#93; DEBUG - SessionImpl.disconnect&#40;3383&#41; | disconnecting session
         &#91;java&#93; DEBUG - SessionImpl.afterTransactionCompletion&#40;596&#41; | transaction completion
         &#91;java&#93; java.lang.StackOverflowError
         &#91;java&#93; Exception in thread "main"
         &#91;java&#93; Java Result&#58; 1
    My Client.java
    Code:
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.abc.persistence.Country;
    import com.abc.service.TerritorialDataService;
    
    public class GeographicalServiceClient 
    &#123;
    	public static ClassPathXmlApplicationContext applicationContext = null;
    	
    	public static void main&#40;String&#91;&#93; args&#41; 
    	&#123;
    		System.out.println&#40;"Starting Geographical Service Client"&#41;;
    		
    		try
    		&#123;
    			String&#91;&#93; contextDefinitions = 
    				new String&#91;&#93;&#123;"application.xml"&#125;;
    			
    			applicationContext = 
    				new ClassPathXmlApplicationContext&#40;contextDefinitions&#41;;
    			
    			System.out.println&#40;"Application Context Loaded &#58;-&#41;"&#41;;
    			
    			TerritorialDataService service = 
    					&#40;TerritorialDataService&#41; applicationContext
    						.getBean&#40;"territorialDataService"&#41;;
    			
    			String countryID = service.addCountry&#40;&#41;;
    			
    			System.out.println&#40;"Country ID = " + countryID &#41;;
    			
    			Country country = service.getCountry&#40; countryID &#41;;
    			
    			if&#40; country != null &#41;
    			&#123;
    				System.out.println&#40;"Label&#58; " + country.getLabel&#40;&#41; &#41;;
    				
    				System.out.println&#40;"Trying to add a State"&#41;;
    				
    				String stateID = 
    					service.addState&#40; countryID, "Uttar Pradesh" &#41;;
    				
    				System.out.println&#40; stateID &#41;;
    				
    				Set states = country.getStates&#40;&#41;;
    				
    				Object&#91;&#93; stateArray = states.toArray&#40;&#41;;
    				
    				State reference = &#40;State&#41; stateArray&#91;0&#93;;
    				
    				System.out.println&#40;"State Label&#58; " + reference.getLabel&#40;&#41; &#41;;
    				
    				State state = service.getState&#40; stateID &#41;;
    				
    				System.out.println&#40;"State Label&#58; " + state.getLabel&#40;&#41; &#41;;
    			&#125;
    			
    		&#125;
    		catch&#40; Exception excp &#41;
    		&#123;
    			excp.printStackTrace&#40;&#41;;
    		&#125;
    		
    		System.out.println&#40;"End Geographical Service Client"&#41;;
    	&#125;
    &#125;
    My DAO Impl:
    Code:
    package com.abc.service.realization;
    
    import java.util.List;
    
    import org.springframework.orm.hibernate.HibernateTemplate;
    import org.springframework.orm.hibernate.support.HibernateDaoSupport;
    
    import com.abc.exceptions.NonExistentParentException;
    import com.abc.persistence.Country;
    import com.abc.persistence.State;
    import com.abc.service.TerritorialDataService;
    
    public class TerritorialDataServiceProvider extends HibernateDaoSupport 
    implements TerritorialDataService 
    &#123;
    	public Country getCountry&#40;String countryID&#41; 
    	&#123;
    		
    		return &#40;Country&#41; getHibernateTemplate&#40;&#41;.load&#40; Country.class, Long.valueOf&#40;countryID&#41; &#41;;
    	&#125;
    
    	public State getState&#40;String stateID&#41; 
    	&#123;
    		return &#40;State&#41; getHibernateTemplate&#40;&#41;.load&#40; State.class, Long.valueOf&#40;stateID&#41; &#41;;
    	&#125;
    
    	public String addCountry&#40;&#41; 
    	&#123;
    		Country country = new Country&#40;&#41;;
    		
    		country.setLabel&#40;"ABC"&#41;;
    		
    		getHibernateTemplate&#40;&#41;.saveOrUpdate&#40; country &#41;;
    		
    		return country.getCountryID&#40;&#41;.toString&#40;&#41;;
    	&#125;
    
    	public String addState&#40;String countryID, String label&#41;
    			throws NonExistentParentException 
    	&#123;
    		final Country country = getCountry&#40; countryID &#41;;
    		
    		if&#40; country == null &#41;
    		&#123;
    			throw new NonExistentParentException&#40;"The Country identified " +
    					"by the supplied countryID &#40;" + countryID + "&#41; " +
    							"does not exist"&#41;;
    		&#125;
    		
    		System.out.println&#40;"Country Id = " + country.getCountryID&#40;&#41; &#41;;
    		
    		State stateToBeAdded = new State&#40;&#41;;
    		
    		stateToBeAdded.setLabel&#40; label &#41;;
    		
    		country.addState&#40; stateToBeAdded &#41;;
    
    		// I expect that since I have used all-delete-orphan in mapping
    		// this would save the state as well.
    		// However, even explicit saving is also not working &#58;-&#40;
    		getHibernateTemplate&#40;&#41;.saveorUpdate&#40; country &#41;;
    		
    		return stateToBeAdded.getStateID&#40;&#41;.toString&#40;&#41;;
    	&#125;
    &#125;
    My applicationContext.xml (the Spring bean configuration file)
    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>
    	
    	<!-- Data Source Configuration -->
        <bean id="dataSource" 
        	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
            <property name="url">
            	<value>jdbc&#58;mysql&#58;//localhost&#58;3306/abcDB</value>
            </property>
            <property name="username" value="root"/>
    		<property name="password" value="abc"/>
        </bean>
        
        <!-- Hibernate Properties -->
        <bean id="hibernateProperties" 
        	class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    		<property name="properties">
    			<props>
    				<prop key="hibernate.hbm2ddl.auto">create</prop>
    				<prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.query.substitutions">true 'T', false 'F'</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.c3p0.minPoolSize">5</prop>
    				<prop key="hibernate.c3p0.maxPoolSize">20</prop>
    				<prop key="hibernate.c3p0.timeout">600</prop>
    				<prop key="hibernate.c3p0.max_statement">50</prop>
    				<prop key="hibernate.c3p0.testConnectionOnCheckout">false</prop>
    			</props>
    		</property>
    	</bean>
        
         <!-- Hibernate SessionFactory -->
        <bean id="hibernateSessionFactory" 
        	class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
            <property name="dataSource">
            	<ref local="dataSource"/>
            </property>
            <property name="hibernateProperties">
            	<ref local="hibernateProperties"/>
            </property>
            <property name="mappingResources">
                <list>
                    <value>Country.hbm.xml</value>
                    <value>State.hbm.xml</value>
                </list>
            </property>
        </bean>
        
        <!-- Transaction manager for a single Hibernate SessionFactory &#40;alternative to JTA&#41; -->
        <bean id="transactionManager" 
        	class="org.springframework.orm.hibernate.HibernateTransactionManager">
       		 <property name="sessionFactory">
            	<ref local="hibernateSessionFactory"/>
            </property>
        </bean>
        
        <!-- This bean implements the functionality. 
        	It might depend upon other beans, as the application scope increase -->
    	<bean id="territorialDataServiceProvider" 
        	class="com.abc.service.realization.TerritorialDataServiceProvider">
             <property name="sessionFactory">
            	<ref local="hibernateSessionFactory"/>
            </property>
        </bean>
    
    	<!-- This bean is the interface -->
        <bean id="territorialDataService" 
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
            	<ref local="transactionManager"/>
            </property>
            <property name="target">
            	<ref local="territorialDataServiceProvider"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="add*">PROPAGATION_REQUIRED</prop>
                    <prop key="change*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
    </beans>
    My .hbm.xml
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http&#58;//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
    
    <hibernate-mapping>
    	<class
    		name="com.abc.persistence.Country" table="COUNTRY">
    		<id name="countryID" column="COUNTRY_ID" type="java.lang.Long">
    			<generator class="native"/>
    		</id>
    		<property 
    			name="label" 
    			type="string" 
    			length="40"
    			not-null="true"/>
    		<set 
    			name="states"
    			inverse="true"
    			lazy="true"
    			cascade="all-delete-orphan">
    			<key column="COUNTRY_ID"/>
    			<one-to-many class="com.abc.persistence.State"/>
    		</set>
    	</class>
    	<query name="findCountryByID">
    		<!&#91;CDATA&#91;  from Country country where country.countryID = &#58;countryID &#93;&#93;>
    	</query>
    </hibernate-mapping>
    
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http&#58;//hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
    
    <hibernate-mapping>
    	<class
    		name="com.abc.persistence.State" table="STATE">
    		<id name="stateID" column="STATE_ID" type="java.lang.Long">
    			<generator class="native"/>
    		</id>
    		<property 
    			name="label" 
    			type="string" 
    			length="40"
    			not-null="true"/>
    		<many-to-one
    			name="country"
    			column="COUNTRY_ID"
    			class="com.abc.persistence.Country"
    			not-null="true" 
    			cascade="none"/>
    		<set 
    			name="provinces"
    			inverse="true"
    			lazy="true"
    			cascade="all-delete-orphan">
    			<key column="STATE_ID"/>
    			<one-to-many class="com.abc.persistence.ProvinceClass"/>
    		</set>
    	</class>
    	<query name="findStateByID">
    		<!&#91;CDATA&#91; from State state where state.stateID = &#58;stateID &#93;&#93;>
    	</query>
    </hibernate-mapping>
    And finally the PoJos'
    Code:
    package com.abc.persistence;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Country extends BaseObject
    &#123;
    	private static final long serialVersionUID = 1L;
    	
    	private Long 	countryID;
    	private String 	label;
    	private Set 	states = new HashSet&#40; 7 &#41;;
    	
    	protected void setCountryID&#40; Long countryID &#41;
    	&#123;
    		this.countryID = countryID;
    	&#125;
    	
    	public Long getCountryID&#40;&#41;
    	&#123;
    		return countryID;
    	&#125;
    	
    	public void setLabel&#40; String label &#41;
    	&#123;
    		this.label = label;
    	&#125;
    	
    	public String getLabel&#40;&#41;
    	&#123;
    		return label;
    	&#125;
    	
    	public void setStates&#40; Set states &#41;
    	&#123;
    		this.states = states;
    	&#125;
    	
    	public Set getStates&#40;&#41;
    	&#123;
    		return states;
    	&#125;
    	
    	public void addState&#40; State state &#41;
    	&#123;
    		if&#40; state == null &#41;
    		&#123;
    			throw new IllegalArgumentException&#40;"State reference is null."&#41;;
    		&#125;
    		
    		final Country country = state.getCountry&#40;&#41;;  
    		
    		if&#40; country != null &#41;
    		&#123;
    			country.getStates&#40;&#41;.remove&#40; state &#41;;
    		&#125;
    			
    		state.setCountry&#40; this &#41;;
    		
    		states.add&#40; state &#41;;
    	&#125;
    &#125;
    
    package com.abc.persistence;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class State extends BaseObject
    &#123;
    	private static final long serialVersionUID = 1L;
    	
    	private Long 		stateID;
    	private String 		label;
    	private Country 	country;
    	private Set 		provinces = new HashSet&#40; 10 &#41;;
    	
    	protected void setStateID&#40; Long stateID &#41;
    	&#123;
    		this.stateID = stateID;
    	&#125;
    	
    	public Long getStateID&#40;&#41;
    	&#123;
    		return stateID;
    	&#125;
    	
    	public void setLabel&#40; String label &#41;
    	&#123;
    		this.label = label;
    	&#125;
    	
    	public String getLabel&#40;&#41;
    	&#123;
    		return label;
    	&#125;
    	
    	public void setCountry&#40; Country country &#41;
    	&#123;
    		this.country = country;
    	&#125;
    	
    	public Country getCountry&#40;&#41;
    	&#123;
    		return country;
    	&#125;
    	
    	public void setProvinces&#40; Set provinces &#41;
    	&#123;
    		this.provinces = provinces;
    	&#125;
    	
    	public Set getProvinces&#40;&#41;
    	&#123;
    		return provinces;
    	&#125;
    	
    	public void addProvince&#40; Province province &#41;
    	&#123;
    		if&#40; province == null &#41;
    		&#123;
    			throw new IllegalArgumentException&#40;"Province reference is null."&#41;;
    		&#125;
    		
    		final State state = province.getState&#40;&#41;;  
    		
    		if&#40; state != null &#41;
    		&#123;
    			state.getProvinces&#40;&#41;.remove&#40; province &#41;;
    		&#125;
    			
    		province.setState&#40; this &#41;;
    		
    		provinces.add&#40; province &#41;;
    	&#125;
    &#125;
    
    package com.abc.persistence;
    
    import java.io.Serializable;
    
    import org.apache.commons.lang.builder.EqualsBuilder;
    import org.apache.commons.lang.builder.HashCodeBuilder;
    import org.apache.commons.lang.builder.ToStringBuilder;
    import org.apache.commons.lang.builder.ToStringStyle;
    
    public class BaseObject implements Serializable 
    &#123;
    	private static final long serialVersionUID = 1L;
    
    	public String toString&#40;&#41; 
    	&#123;
    		return 
    			ToStringBuilder.reflectionToString&#40; this, 
    					ToStringStyle.MULTI_LINE_STYLE &#41;;
    	&#125;
    
    	public boolean equals&#40; Object obj &#41;
    	&#123;
    		return 
    			EqualsBuilder.reflectionEquals&#40; this, obj &#41;;
    	&#125;
    	
    	public int hashCode&#40;&#41;
    	&#123;
    		return
    			HashCodeBuilder.reflectionHashCode&#40; this &#41;;
    	&#125;
    &#125;

  • #2
    public String addState(String countryID, String label)
    throws NonExistentParentException
    {
    final Country country = getCountry( countryID );

    if( country == null )
    {
    throw new NonExistentParentException("The Country identified " +
    "by the supplied countryID (" + countryID + ") " +
    "does not exist");
    }

    System.out.println("Country Id = " + country.getCountryID() );

    State stateToBeAdded = new State();

    stateToBeAdded.setLabel( label );

    country.addState( stateToBeAdded );

    // I expect that since I have used all-delete-orphan in mapping
    // this would save the state as well.
    // However, even explicit saving is also not working :-(
    getHibernateTemplate().saveorUpdate( country );

    return stateToBeAdded.getStateID().toString();
    }
    I think the stateToBeAdded.getStateID() is equal to NULL since there is nothing in the code that set the stateId.



    [/quote]

    Comment


    • #3
      stateID might not be the culprit

      Thanks Noel,

      But I believe that stateID is set by Hibernate after the object is entered into the database, as is in the case of countryID. The id-generation strategy is native.

      However, if I remove this line, I get a StackOverflowException... and just this message and nothing else... so I am not able to debug...

      Thanks once again,
      Vaibhav

      Comment

      Working...
      X