Announcement Announcement Module
Collapse
No announcement yet.
Get Error When Use BlobByteArrayType with hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get Error When Use BlobByteArrayType with hibernate

    I am using SpringFramework 1.1.1 and Hibernate 2.1.6

    I config the following in applicationContext-hibernate.xml

    Code:
        <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    
    	    <property name="lobHandler"><ref bean="oracleLobHandler"/></property>
            <property name="mappingResources">
                <list>
                    <value>jp/co/nec/panfocus/domain/UserBase.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
                    <prop key="hibernate.query.substitutions">true=1 false=0</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
                    <prop key="hibernate.connection.url">jdbc&#58;oracle&#58;thin&#58;@DN2003&#58;1521&#58;V250BUN</prop>
                    <prop key="hibernate.connection.username">EXAMPLE</prop>
                    <prop key="hibernate.connection.password">EXAMPLE</prop>
                </props>
            </property>
        </bean>
        <bean id="oracleLobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"/>
    in UserBase.hbm.xml

    Code:
        <property
            name="image"
            type="org.springframework.orm.hibernate.support.BlobByteArrayType"
            column="IMAGE"
            length="4000"
        >
            <meta attribute="field-description">
               @hibernate.property
                column="IMAGE"
                length="4000"
            </meta>    
        </property>
    in UserBase.java

    Code:
        
    public byte&#91;&#93; getImage&#40;&#41; &#123;
            return this.image;
        &#125;
    
        public void setImage&#40;byte&#91;&#93; image&#41; &#123;
            this.image = image;
        &#125;
    then I write the following Test
    Code:
    	public void testInsertBlob&#40;&#41; throws Exception &#123;
    
    		SessionFactory sf = &#40;SessionFactory&#41; context.getBean&#40;"sessionFactory"&#41;;
    
    		Session s = sf.openSession&#40;&#41;;
    		Transaction tx = null;
    		try &#123;
    			tx = s.beginTransaction&#40;&#41;;
    			UserBase ub = new UserBase&#40;&#41;;
    			ub.setId&#40;new Long&#40;2&#41;&#41;;
    			ub.setUserName&#40;"HelloName"&#41;;
    			ub.setPasswordHash&#40;"XXXXXXXXX"&#41;;
    			ub.setImage&#40;new byte&#91;&#93;&#123;1,2,3&#125;&#41;;// or some file bytes.
    			s.save&#40;ub&#41;;
    			s.flush&#40;&#41;;
    			tx.commit&#40;&#41;;
    		&#125; catch &#40;Exception e&#41; &#123;
    			if &#40;tx != null&#41; tx.rollback&#40;&#41;;
    			throw e;
    		&#125; finally &#123;
    			s.close&#40;&#41;;
    		&#125;
    
    	&#125;
    and I got


    Code:
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.SessionImpl - Flushed&#58; 0 &#40;re&#41;creations, 0 updates, 0 removals to 0 collections
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.Printer - listing entities&#58;
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.Printer - jp.co.nec.panfocus.domain.UserBase&#123;userName=HelloName, passwordHash=XXXXXXXXX, image=&#91;B@983d95, id=2&#125;
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.SessionImpl - executing flush
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity&#58; &#91;jp.co.nec.panfocus.domain.UserBase#2&#93;
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.BatcherImpl - about to open&#58; 0 open PreparedStatements, 0 open ResultSets
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.SQL - insert into USER_BASE &#40;USER_NAME, PASSWORD_HASH, IMAGE, ID&#41; values &#40;?, ?, ?, ?&#41;
    Hibernate&#58; insert into USER_BASE &#40;USER_NAME, PASSWORD_HASH, IMAGE, ID&#41; values &#40;?, ?, ?, ?&#41;
    2004-10-09 11&#58;21&#58;24,078 DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement
    2004-10-09 11&#58;21&#58;24,109 DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity&#58; &#91;jp.co.nec.panfocus.domain.UserBase#2&#93;
    2004-10-09 11&#58;21&#58;24,109 DEBUG net.sf.hibernate.type.StringType - binding 'HelloName' to parameter&#58; 1
    2004-10-09 11&#58;21&#58;24,109 DEBUG net.sf.hibernate.type.StringType - binding 'XXXXXXXXX' to parameter&#58; 2
    2004-10-09 11&#58;21&#58;24,234 DEBUG org.springframework.jdbc.support.lob.OracleLobHandler - Created new Oracle LOB
    2004-10-09 11&#58;21&#58;24,234 DEBUG org.springframework.jdbc.support.lob.OracleLobHandler - Set bytes for BLOB with length 3
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.transaction.JDBCTransaction - rollback
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.impl.SessionImpl - closing session
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size&#58; 1
    2004-10-09 11&#58;21&#58;24,250 DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion
    ------------- ---------------- ---------------
    
    Testcase&#58; testInsertBlob took 2.047 sec
    	Caused an ERROR
    Active Spring transaction synchronization or jtaTransactionManager on LocalSessionFactoryBean plus active JTA transaction required
    java.lang.IllegalStateException&#58; Active Spring transaction synchronization or jtaTransactionManager on LocalSessionFactoryBean plus active JTA transaction required
    	at org.springframework.orm.hibernate.support.AbstractLobType.nullSafeSet&#40;AbstractLobType.java&#58;169&#41;
    	at net.sf.hibernate.type.CustomType.nullSafeSet&#40;CustomType.java&#58;118&#41;
    	at net.sf.hibernate.persister.EntityPersister.dehydrate&#40;EntityPersister.java&#58;393&#41;
    	at net.sf.hibernate.persister.EntityPersister.insert&#40;EntityPersister.java&#58;466&#41;
    	at net.sf.hibernate.persister.EntityPersister.insert&#40;EntityPersister.java&#58;442&#41;
    	at net.sf.hibernate.impl.ScheduledInsertion.execute&#40;ScheduledInsertion.java&#58;29&#41;
    	at net.sf.hibernate.impl.SessionImpl.executeAll&#40;SessionImpl.java&#58;2418&#41;
    	at net.sf.hibernate.impl.SessionImpl.execute&#40;SessionImpl.java&#58;2371&#41;
    	at net.sf.hibernate.impl.SessionImpl.flush&#40;SessionImpl.java&#58;2240&#41;
    	at jp.co.nec.panfocus.hibernate.BlobByteArrayTypeTests.testInsertBlob&#40;BlobByteArrayTypeTests.java&#58;38&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke0&#40;Native Method&#41;
    	at sun.reflect.NativeMethodAccessorImpl.invoke&#40;NativeMethodAccessorImpl.java&#58;39&#41;
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke&#40;DelegatingMethodAccessorImpl.java&#58;25&#41;
    Am I do right thing for using the "org.springframework.orm.hibernate.support.BlobByt eArrayType",
    Thanks in advance!!

  • #2
    You shouldn't be using Hibernate's (via the Hibernate Session) programmatic transaction demarcation code.

    You should be using either Spring's declarative or programmatic transaction support, or alternately as the error message you are using indicates, you can also get by with just JTA transactions and no Spring transactions at all (as long as the session factory in this case is condifured to be able to look up the JTA Transaction Manager).

    For information on using transactions in Spring, please see the relevant section of the reference manual. Ditto for the HIbernate support.

    Comment


    • #3
      Colin, I am getting the same error.

      As you suggested I am using Hibernate declarative transaction. I got this error:

      Active Spring transaction synchronization or active JTA transaction with 'jtaTransactionManager' on LocalSessionFactoryBean required

      I am using:
      <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
      <property name="sessionFactory"><ref local="sessionFactory"/></property>
      </bean>


      I checked the hibernate reference - I did not find how to get "active spring transacton synchronization"?


      The documents say that the initSynchronization will be called automatically...

      Comment


      • #4
        I realized my mistake:

        I was wrapping AOP transaction around business object instead of DAO object. So the DAO object was never under transaction. That's why the DAO unit test failed. After changing the application-context, it works.

        Thanks.

        Comment


        • #5
          same problem

          hi,

          I am having the exact same problem, can you show me exactly what you did in your application-context to make it work? I must be close, but I am probably just missing a little bit.

          Thanks!

          Comment

          Working...
          X