当前位置: 代码迷 >> J2EE >> 使用Hibernate向ORACLE数据库保存Blob字段的有关问题
  详细解决方案

使用Hibernate向ORACLE数据库保存Blob字段的有关问题

热度:366   发布时间:2016-04-22 03:33:27.0
使用Hibernate向ORACLE数据库保存Blob字段的问题
我使用以下的语句保存一个newPhoto实体,其中image字段为Blob类型  

  Session session= HibernateSessionFactory.getSession();
Transaction tx=null;
Boolean flag=false;
try {
tx = session.beginTransaction();
//执行事务
newPhoto tempP=new newPhoto();
//先建立空的栏位
tempP.setImage(Hibernate.createBlob(new byte[1]));
System.out.println("开始保存空newPhoto");
session.save(tempP);
System.out.println("保存空newPhoto完毕");
// 执行flush,让Hibernate INSERT 空栏位
session.flush();
System.out.println("flush()完毕");
// 执行refresh,让Hibernate执行SELECT FOR UPDATE
session.refresh(tempP, LockMode.UPGRADE);
System.out.println("refresh()完毕");
tempP=p;
//session.save(tempP);
//提交事务
tx.commit();
flag=true;
 
}

执行到session.save(tempP);时就rollback了,也没报错,不知是什么原因?后台输出如下:
开始保存空newPhoto
17:11:00,500 DEBUG DefaultSaveOrUpdateEventListener:158 - saving transient instance
17:11:00,516 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
17:11:00,516 DEBUG SQL:401 - select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
17:11:00,516 DEBUG AbstractBatcher:484 - preparing statement
17:11:01,078 DEBUG SequenceGenerator:82 - Sequence identifier generated: 44
17:11:01,078 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
17:11:01,078 DEBUG AbstractBatcher:533 - closing statement
17:11:01,078 DEBUG AbstractSaveEventListener:112 - generated identifier: 44, using strategy: org.hibernate.id.SequenceGenerator
17:11:01,078 DEBUG AbstractSaveEventListener:153 - saving [mq.newPhoto#44]
17:11:01,110 DEBUG JDBCTransaction:152 - rollback
17:11:01,110 DEBUG JDBCTransaction:163 - rolled back JDBC Connection
17:11:01,110 DEBUG JDBCContext:215 - after transaction completion
17:11:01,110 DEBUG ConnectionManager:404 - aggressively releasing JDBC connection
17:11:01,125 DEBUG ConnectionManager:441 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
17:11:01,125 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
17:11:01,125 DEBUG SessionImpl:422 - after transaction completion
17:11:01,125 DEBUG SessionImpl:273 - closing session
17:11:01,125 DEBUG ConnectionManager:375 - connection already null in cleanup : no action


newPhoto的映谢文件如下:
<hibernate-mapping>
  <class name="mq.newPhoto" table="zy_photo">
   
  <id name="id" column="ID" type="long">
  <generator class="sequence"/>
  </id>
   
  <property name="title" column="title" type="string" not-null="true" />
  <property name="type" /> 
  <property name="hits" type="int" /> 
  <property name="description" />
  <property name="image">
  <column name="image" sql-type="BLOB"/>
  </property>
  <property name="putTime" type="timestamp"/>
   
  </class>
</hibernate-mapping>


------解决方案--------------------
2次save?
------解决方案--------------------
try { 
  相关解决方案