I am using SpringFramework 1.1.1 and Hibernate 2.1.6
I config the following in applicationContext-hibernate.xml
in UserBase.hbm.xmlCode:<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:oracle:thin:@DN2003:1521: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.javaCode:<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>
then I write the following TestCode:public byte[] getImage() { return this.image; } public void setImage(byte[] image) { this.image = image; }
and I gotCode:public void testInsertBlob() throws Exception { SessionFactory sf = (SessionFactory) context.getBean("sessionFactory"); Session s = sf.openSession(); Transaction tx = null; try { tx = s.beginTransaction(); UserBase ub = new UserBase(); ub.setId(new Long(2)); ub.setUserName("HelloName"); ub.setPasswordHash("XXXXXXXXX"); ub.setImage(new byte[]{1,2,3});// or some file bytes. s.save(ub); s.flush(); tx.commit(); } catch (Exception e) { if (tx != null) tx.rollback(); throw e; } finally { s.close(); } }
Am I do right thing for using the "org.springframework.orm.hibernate.support.BlobByt eArrayType",Code:2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.SessionImpl - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.Printer - listing entities: 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.Printer - jp.co.nec.panfocus.domain.UserBase{userName=HelloName, passwordHash=XXXXXXXXX, image=[B@983d95, id=2} 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.SessionImpl - executing flush 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.persister.EntityPersister - Inserting entity: [jp.co.nec.panfocus.domain.UserBase#2] 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.BatcherImpl - about to open: 0 open PreparedStatements, 0 open ResultSets 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.SQL - insert into USER_BASE (USER_NAME, PASSWORD_HASH, IMAGE, ID) values (?, ?, ?, ?) Hibernate: insert into USER_BASE (USER_NAME, PASSWORD_HASH, IMAGE, ID) values (?, ?, ?, ?) 2004-10-09 11:21:24,078 DEBUG net.sf.hibernate.impl.BatcherImpl - preparing statement 2004-10-09 11:21:24,109 DEBUG net.sf.hibernate.persister.EntityPersister - Dehydrating entity: [jp.co.nec.panfocus.domain.UserBase#2] 2004-10-09 11:21:24,109 DEBUG net.sf.hibernate.type.StringType - binding 'HelloName' to parameter: 1 2004-10-09 11:21:24,109 DEBUG net.sf.hibernate.type.StringType - binding 'XXXXXXXXX' to parameter: 2 2004-10-09 11:21:24,234 DEBUG org.springframework.jdbc.support.lob.OracleLobHandler - Created new Oracle LOB 2004-10-09 11:21:24,234 DEBUG org.springframework.jdbc.support.lob.OracleLobHandler - Set bytes for BLOB with length 3 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.transaction.JDBCTransaction - rollback 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.impl.SessionImpl - closing session 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.impl.SessionImpl - disconnecting session 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1 2004-10-09 11:21:24,250 DEBUG net.sf.hibernate.impl.SessionImpl - transaction completion ------------- ---------------- --------------- Testcase: testInsertBlob took 2.047 sec Caused an ERROR Active Spring transaction synchronization or jtaTransactionManager on LocalSessionFactoryBean plus active JTA transaction required java.lang.IllegalStateException: Active Spring transaction synchronization or jtaTransactionManager on LocalSessionFactoryBean plus active JTA transaction required at org.springframework.orm.hibernate.support.AbstractLobType.nullSafeSet(AbstractLobType.java:169) at net.sf.hibernate.type.CustomType.nullSafeSet(CustomType.java:118) at net.sf.hibernate.persister.EntityPersister.dehydrate(EntityPersister.java:393) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:466) at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:442) at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2418) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2371) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2240) at jp.co.nec.panfocus.hibernate.BlobByteArrayTypeTests.testInsertBlob(BlobByteArrayTypeTests.java:38) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
Thanks in advance!!


Reply With Quote