Hibernate com campo Blob no Oracle

2 respostas
agranado2k

Fala Galera do portal!!!!

Tenho um problema para compartilhar com a galera, e talvez alguém consiga achar o porquê....

Achei em um forum sobre o hibernate uma implementação do campo Blob no Oracle, ela funciona muito bem... exceto que.. em algumas vezes, quando o arquivo é grande, maior que 5k, e tento persistir o objeto que possui o campo blob... parece que o hibernate dá uma travada... ou entre em um loop infinito...
Bom... o meu mapeamento e a classe estão abaixo... espero ter conseguido explicar meu problema... se alguém já tiver passado por isso... e/ou souber o porquê... por favor poste aki!!!!! :grin:

Ah... antes que alguém diga para aumentar o tamanho do buffer... eu já tentei isso... :grin:

abraços a todos!!!!!!

Classe
package model;

import JHibernateGenericDAO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;


import java.util.Vector;
import org.hibernate.Hibernate;

public class JPropertyValue
{
  
    private int id;
    private JProperty property;
    private String value;    
    private byte[] image;
  
    /** Creates a new instance of JPropertyValue */
    public JPropertyValue()
    {
    }
       
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public JProperty getProperty() {
        return property;
    }

    public void setProperty(JProperty property) {
        this.property = property;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
    
    public String toString() {
        return this.value;
    }  

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }
    
     /** Não chame esse método.  Usado apenas pelo Hibernate. */
    public void setImageBlob(Blob imageBlob) {
        if (null != imageBlob)
            this.image = this.toByteArray(imageBlob);
        else
            this.image = null;
    }
    
    /** Não chame esse método.  Usado apenas pelo Hibernate. */
    public Blob getImageBlob() {
        if(null != this.image)
            return Hibernate.createBlob(this.image); 
        else
            return null;
    } 
    
    private byte[] toByteArray(Blob fromBlob) { 
        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        try {  
            return toByteArrayImpl(fromBlob, baos); 
        } 
        catch (SQLException e) {
            throw new RuntimeException(e); 
        } 
        catch (IOException e) {
            throw new RuntimeException(e);
        } 
        finally { 
            if (baos != null) { 
                try { 
                    baos.close(); 
                }
                catch (IOException ex) {
                }
            }  
        }
    }
    
    private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)  throws SQLException, IOException {
        byte[] buf = new byte[4000];
        InputStream is = fromBlob.getBinaryStream();
        try {   
            for (;;) { 
                int dataSize = is.read(buf);
                if (dataSize == -1)   
                    break;  
                baos.write(buf, 0, dataSize); 
            } 
        } 
        finally {
            if (is != null) { 
                try {
                    is.close(); 
                }
                catch (IOException ex) { 
                } 
            } 
        } 
        return baos.toByteArray();
    }
    
}
Mapeamento
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="JPropertyValue" table="PROPRIEDADE_VALOR" dynamic-update="true" dynamic-insert="true">
		<!-- Identificador da classe -->
		<id name="id" column="PRVA_CD_CHAVE" type="integer">
		     <generator class="increment"/>
		</id>
                
                <!-- Propriedades -->
                <property name="imageBlob"  column="PRVA_MM_ARQUIVO"     type="blob"/>
                <property name="value"      column="PRVA_NM_NOME"                   />
                
                <!-- Relacionamentos da classe -->
                <!-- Com JProperty -->
                <many-to-one name="property" class="JProperty"
                             cascade="none" fetch="join" update="true" insert="true"
                             column="PROP_CD_CHAVE"
                />
                
	</class>
</hibernate-mapping>

2 Respostas

_fs

http://www.guj.com.br/posts/list/16617.java

agranado2k

Fala LIPE!!!

Cara… eu tentei usar o esquema do link do forum que vc passou… e nem funcionar conseguir fazer…eheheeheh…

O meu que eu que tá aí em cima funciona… mas em algumas vezes… quando o arquivo é grande… ele(hibernate) parece que trava… ou entra em loop… tb já tentei aumentar o tamanho do array de bytes… e continuava na mesma… ahhhh… não tenho a menor noção o do porquê disso… eheheh…

Se algué tiver alguma outra diga me falem… :slight_smile:

abraços a todos!!! e valew pela força LIPE!!!

Criado 27 de março de 2006
Ultima resposta 28 de mar. de 2006
Respostas 2
Participantes 2