Hibernate 2 + Oracle 9 + BLOB

1 resposta
F

Pessoal,

Pra variar, mais um tendo problemas com esse inferno de BLOB.

Ja tentei varias opções (tem algumas comentadas), mas sempre tenho erros de ClassCastException quando tento sair do java.sql.Blob para o oracle.sql.BLOB.

Alguem que já passou por isso e resolveu seus problemas ?

Ps. Abaixo tem um JPG do meu debug, onde o t.getStream() já está retornando um oracle.sql.BLOB.

public void salvaTeste(byte[] binario, String desc) throws BusinessException
	{
        Session s = null;
        Transaction tx = null;
        
        teste t = new teste();
        t.setDesc(desc);
        
        try{
        	s = getSession();
	        tx = s.beginTransaction();	        
	        t.setStream(Hibernate.createBlob(new byte[]{0}));
	        s.saveOrUpdate(t);
	        tx.commit();
	        s.close();

	        s = this.getSession();
	        tx = s.beginTransaction();
	        
	        t = (teste) s.load( teste.class, t.getId(), LockMode.UPGRADE );
	        
	        //Connection conn = s.connection().getMetaData().getConnection();
	        //BLOB blob = BLOB.createTemporary(conn, false, BLOB.DURATION_SESSION);
	        
	        //java.sql.Blob javaBlob = t.getStream();
	        //oracle.sql.BLOB blob = (oracle.sql.BLOB)javaBlob;
	        
	        SerializableBlob sBlob = (SerializableBlob) t.getStream();
	        oracle.sql.BLOB blob = (oracle.sql.BLOB)sBlob.getWrappedBlob();
	        
	        OutputStream pw = blob.getBinaryOutputStream();
	        pw.write(binario);
	        pw.close(); 
	        tx.commit();
	        s.close();
        }    	
        catch(Exception e)
        {
        	e.printStackTrace();
        	checkTransaction(tx,e);
    	} 
		finally 
		{
			closeSession(s);
		}

	}


1 Resposta

F

Achei uma solução … Retirar a parte ORACLE do negocio…

:slight_smile:

s = getSession();
	        tx = s.beginTransaction();	        
	        t.setStream(Hibernate.createBlob(new byte[]{0}));
	        s.saveOrUpdate(t);
	        tx.commit();
	        s.close();

	        s = this.getSession();
	        tx = s.beginTransaction();	        
	        t = (teste) s.load( teste.class, t.getId(), LockMode.UPGRADE );

	        java.sql.Blob javaBlob = t.getStream();
	        OutputStream pw = javaBlob.setBinaryStream(0);
	        pw.write(binario);
	        pw.close(); 
	        tx.commit();
	        s.close();

Incrivelmente, funcional ao contrario do que é dito em tudo que é lugar.

Att.,

Criado 23 de fevereiro de 2007
Ultima resposta 26 de fev. de 2007
Respostas 1
Participantes 1