首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

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

2012-01-13 
使用Hibernate向ORACLE数据库保存Blob字段的问题我使用以下的语句保存一个newPhoto实体,其中image字段为Bl

使用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 { 


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);
//当你锁定记录后 就通过这个对象获得他的 Blob; //xxx是你那个Blob 的字段
oracle.sql.BlOB clob = (oracle.sql.BlOB ) tempP.getXXX();
//然后获得流. //后面是什么我也记不太清楚. 你点出来看一下就是那个返回writer对象的就是了。
java.io.Writer pw = blob.getXXXXXXXXXXXX();

pw.writer(你的字节数组);
pw.close();

//提交事物就可以了.不要再次save()了
tx.commit();
flag=true; 


热点排行