public void importSiteBook()//被main()调用的方法,肯定连接上数据库了
{
Session sess1 = null;
Session sess2 = null;
Transaction tr = null;
List list = null; 
try{
sess1 = HibernateSessionFactoryroot.currentSession();//原表的HibernateSessionFactory
String sql = "from SiteBook t";//查出所有数据content的类型是string
Query query = sess1.createQuery(sql);
list = query.list();
}
catch (Exception e) {
e.printStackTrace();

} finally {
HibernateSessionFactoryroot.closeSession();//关闭HibernateSessionFactory
}
try{ 
sess2 = HibernateSessionFactoryaim.currentSession();//新表HibernateSessionFactory
    tr = sess2.beginTransaction();
DBroot.SiteBook book1 = new DBroot.SiteBook();

for(int i = 0 ; i < list.size();i++){
DBaim.SiteBook book2 = new DBaim.SiteBook();
book1 = (DBroot.SiteBook) list.get(i);
book2.setDcTitle(book1.getDcTitle());
book2.setDdDate(book1.getDdDate());
book2.setDcWhere(book1.getDcWhere());
book2.setDcPeople(book1.getDcPeople());
book2.setDnCode(book1.getDnCode());
book2.setDcContent(BLOB.empty_lob());//content的类型是blob



sess2.save(book2);
tr.commit();
}//到这都可以正确执行
for(int i =0 ;i<list.size();i++){
DBaim.SiteBook book2 = new DBaim.SiteBook();
book1 = (DBroot.SiteBook) list.get(i);
Blob blob1 = new SerialBlob(book1.getDcContent().getBytes("GB2312"));
book2.setDcContent(blob1);
sess2.update(book2);//无法正确提交
tr.commit();
}
 }catch(Exception e){ 
        System.err.println("数据存储发生错误!"); 
        e.printStackTrace(); 
        tr.rollback();
        } 
 finally {
  HibernateSessionFactoryaim.closeSession();//关闭
}
错误信息:org.hibernate.exception.GenericJDBCException: could not update: [DBaim.SiteBook#0]
at org.hibernate.exception.ErrorCodeConverter.handledNonSpecificException(ErrorCodeConverter.java:92)
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:80)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2000)
at org.hibernate.persister.entity.BasicEntityPersister.updateOrInsert(BasicEntityPersister.java:1909)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:2149)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:75)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:137)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:324)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at main.Import.importSiteBook(Import.java:114)
at main.Import.main(Import.java:28)
Caused by: java.sql.SQLException: An SQLException was provoked by the following failure: java.lang.ClassCastException: javax.sql.rowset.serial.SerialBlob
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:104)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:62)
at com.mchange.v2.c3p0.impl.NewPooledConnection.handleThrowable(NewPooledConnection.java:306)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setBlob(NewProxyPreparedStatement.java:509)
at org.hibernate.type.BlobType.set(BlobType.java:48)
at org.hibernate.type.BlobType.nullSafeSet(BlobType.java:116)
at org.hibernate.persister.entity.BasicEntityPersister.dehydrate(BasicEntityPersister.java:1617)
at org.hibernate.persister.entity.BasicEntityPersister.update(BasicEntityPersister.java:1963)
... 13 more
Caused by: java.lang.ClassCastException: javax.sql.rowset.serial.SerialBlob
at oracle.jdbc.driver.OraclePreparedStatement.setBlob(OraclePreparedStatement.java:2098)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setBlob(NewProxyPreparedStatement.java:495)
... 17 more

解决方案 »

  1.   

     DBaim.SiteBook book2 = new DBaim.SiteBook();
                    book1 = (DBroot.SiteBook) list.get(i);
                    Blob blob1 = new SerialBlob(book1.getDcContent().getBytes("GB2312"));
                    book2.setDcContent(blob1);
                    sess2.update(book2);//无法正确提交
                    tr.commit();
    你的book2是new出来的,哪来update,最多就是save()
      

  2.   


    修改后!!!还是有错误package main;
    import java.io.InputStream;
    import java.sql.Blob;
    import java.util.EmptyStackException;
    import java.util.List;import javax.sql.rowset.serial.SerialBlob;import DBaim.*;
    import DBaim.SiteBook;
    import DBroot.SiteAccount;
    import DBroot.*;
    import oracle.sql.BLOB;import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;import com.microsoft.jdbc.base.BaseBlob;
    import com.microsoft.jdbc.base.BaseBlobInputStream;public class Import {

    public static void main(String args[])
    {
    Import a = new Import();
    a.importSiteBook();
    }
    public void importAccount(){
    Session sess1 = null;
    Session sess2 = null;
    Transaction tr = null;
    List list = null; 
    try{
    sess1 = HibernateSessionFactoryroot.currentSession();
    String sql = "select a from SiteAccount a";
    Query query = sess1.createQuery(sql);
    list = query.list();
    }
    catch (Exception e) {
    e.printStackTrace();

    } finally {
    HibernateSessionFactoryroot.closeSession();
    }
    try{ 
    sess2 = HibernateSessionFactoryaim.currentSession();
        tr = sess2.beginTransaction();
    DBroot.SiteAccount account1 = new DBroot.SiteAccount();

    for(int i = 0 ; i < list.size();i++){
    DBaim.SiteAccount account2 = new DBaim.SiteAccount();
    account1 = (DBroot.SiteAccount) list.get(i);
    account2.setDcName(account1.getDcName());
    account2.setDcPass(account1.getDcPass());
    sess2.save(account2);
    tr.commit();
    }

     }catch(Exception e){ 
            System.err.println("数据存储发生错误!"); 
            e.printStackTrace(); 
            tr.rollback();
            } 
     finally {
      HibernateSessionFactoryaim.closeSession();
    }
        } 
    public void importSiteBook()
    {
    Session sess1 = null;
    Session sess2 = null;
    Transaction tr = null;
    List list = null; 
    try{
    sess1 = HibernateSessionFactoryroot.currentSession();
    String sql = "from SiteBook t";
    Query query = sess1.createQuery(sql);
    list = query.list();
    }
    catch (Exception e) {
    e.printStackTrace();

    } finally {
    HibernateSessionFactoryroot.closeSession();
    }
    try{ 
    sess2 = HibernateSessionFactoryaim.currentSession();
        tr = sess2.beginTransaction();
    DBroot.SiteBook book1 = new DBroot.SiteBook();

    for(int i = 0 ; i < list.size();i++){
    DBaim.SiteBook book2 = new DBaim.SiteBook();
    book1 = (DBroot.SiteBook) list.get(i);
    book2.setDcTitle(book1.getDcTitle());
    book2.setDdDate(book1.getDdDate());
    book2.setDcWhere(book1.getDcWhere());
    book2.setDcPeople(book1.getDcPeople());
    book2.setDnCode(book1.getDnCode());
    book2.setDcContent(BLOB.empty_lob());



    sess2.save(book2);
    tr.commit();
    }
    String sql1 = "from SiteBook t";
    Query query1 = sess2.createQuery(sql1);
    List list2 = query1.list();
    for(int i =0 ;i<list2.size();i++){
    DBaim.SiteBook book2 = new DBaim.SiteBook();
    book2 = (SiteBook) list2.get(i);
    oracle.sql.BLOB imgBlob = (BLOB) book2.getDcContent(); 
    sess2.update(book2);
    tr.commit();
    }
     }catch(Exception e){ 
            System.err.println("数据存储发生错误!"); 
            e.printStackTrace(); 
            tr.rollback();
            } 
     finally {
      HibernateSessionFactoryaim.closeSession();
    }
    }
    public void importSiteBulletin(){
    Session sess1 = null;
    Session sess2 = null;
    Transaction tr = null;
    List list = null; 
    try{
    sess1 = HibernateSessionFactoryroot.currentSession();
    String sql = "from SiteBulletin t";
    Query query = sess1.createQuery(sql);
    list = query.list();
    }
    catch (Exception e) {
    e.printStackTrace();

    } finally {
    HibernateSessionFactoryroot.closeSession();
    }
    try{ 
    sess2 = HibernateSessionFactoryaim.currentSession();
        tr = sess2.beginTransaction();
    DBroot.SiteBulletin bulletin1 = new DBroot.SiteBulletin();

    for(int i = 0 ; i < list.size();i++){
    DBaim.Sitebulletin bulletin2 = new DBaim.Sitebulletin();
    bulletin2.setDdDate(bulletin1.getDdDate());
    bulletin2.setDcTitle(bulletin1.getDcTitle());
    Blob blob = new SerialBlob(bulletin1.getDcContent().getBytes("gb2312"));
    bulletin2.setDcContent(blob);
    sess2.save(bulletin2);
    tr.commit();
    }

     }catch(Exception e){ 
            System.err.println("数据存储发生错误!"); 
            e.printStackTrace(); 
            tr.rollback();
            } 
     finally {
      HibernateSessionFactoryaim.closeSession();
    }

    }
    }错误:
    Hibernate: select sitebook0_.DN_ID as DN1_, sitebook0_.DC_TITLE as DC2_1_, sitebook0_.DD_DATE as DD3_1_, sitebook0_.DC_PEOPLE as DC4_1_, sitebook0_.DC_WHERE as DC5_1_, sitebook0_.DN_CODE as DN6_1_, sitebook0_.DC_CONTENT as DC7_1_ from SITE_BOOK sitebook0_
    Hibernate: select SEQ_SITE_BOOK_ID.nextval from dual
    Hibernate: insert into SITE_BOOK (DC_TITLE, DD_DATE, DC_PEOPLE, DC_WHERE, DN_CODE, DC_CONTENT, DN_ID) values (?, ?, ?, ?, ?, ?, ?)
    Hibernate: select SEQ_SITE_BOOK_ID.nextval from dual
    Hibernate: insert into SITE_BOOK (DC_TITLE, DD_DATE, DC_PEOPLE, DC_WHERE, DN_CODE, DC_CONTENT, DN_ID) values (?, ?, ?, ?, ?, ?, ?)
    Hibernate: select SEQ_SITE_BOOK_ID.nextval from dual
    Hibernate: insert into SITE_BOOK (DC_TITLE, DD_DATE, DC_PEOPLE, DC_WHERE, DN_CODE, DC_CONTENT, DN_ID) values (?, ?, ?, ?, ?, ?, ?)
    Hibernate: select sitebook0_.DN_ID as DN1_, sitebook0_.DC_TITLE as DC2_8_, sitebook0_.DD_DATE as DD3_8_, sitebook0_.DC_PEOPLE as DC4_8_, sitebook0_.DC_WHERE as DC5_8_, sitebook0_.DN_CODE as DN6_8_, sitebook0_.DC_CONTENT as DC7_8_ from SITE_BOOK sitebook0_
    数据存储发生错误!
    java.lang.ClassCastException: org.hibernate.lob.SerializableBlob
    at main.Import.importSiteBook(Import.java:115)
    at main.Import.main(Import.java:29)
      

  3.   

    Blob blob1 = new SerialBlob(book1.getDcContent().getBytes("GB2312"));
    这里出错了 ..SerialBlob不能转换成Blob  ??
    你的Class Blob 有没有implements Serializable
    个人猜测SerialBlob是个Serializable  ..因此 
    试试  class Blob implements Serializable
      

  4.   

    根据2楼的错误 ..更加坚信是没有implements Serializable
      

  5.   

    Blob blob = new SerialBlob(bulletin1.getDcContent().getBytes("gb2312"));
    Blob b = (java.sql.Blob)blob.getWrappedBlob();
    bulletin2.setDcContent(b);如果不行,就看下oracle驱动的版本,换成最新的
    再看看配置文件是如何配置的
      

  6.   

    import oracle.sql.BLOB;

    import javax.sql.rowset.serial.SerialBlob;它有什么关系么?
      

  7.   

    我说的有些问题,弄混了,这里有个解决的
    http://redleaf.javaeye.com/blog/100718
      

  8.   

    一定要用BLOB这个类型,不能改