Hibernate/JPA + Oracle + BLOB

3 respostas
L

Gujeiros de plantão!

Estou utilizando Hibernate/JPA + Oracle e
estou enfrentando um problemão aqui, ao gravar uma imagem na base de dados em um campo blob.

ao persistir o objeto que contém o atributo do tipo byte[], ocorre uma exceção com a seguinte mensagem:

ORA-01460: conversão não-implementada ou não-resolvível solicitada

OBS.: O atributo tipo byte[] possui a anotação @Lob.

Alguém já enfrentou algum problema do tipo???

3 Respostas

P

No mapeamento
@Column(name="conteudo")
//@Transient // será atualizado manualmente por problemas no uso do drive jdbc oracle com hibernate em campos blob
private byte[] conteudoRaw;

Para incluir:
public void incluiImagem(Session sessao,  byte[] conteudoImagem, String creditos, String descricao) throws Exception {

		Imagem imagem = new Imagem();
		imagem.setId(criaId());
		imagem.setConteudoRaw(conteudoImagem);
		imagem.setCreditos(creditos);
		imagem.setDescricao(descricao);
		sessao.inclui(imagem);

	}
L

E aí Puka.

Bom … fiz exatamente como você colocou aí …
e não funcionou.

Se o atributo é @Transient, então significa que não será persistido.

então ao salvar a imagem… o campo “conteudo” fica com 0 bytes no banco de dados.

teria outra alternativa??

L

Na Anotação continua @Lob

@Column(name = “FOTO”)

private byte[] foto;

<a class="mention" href="/u/lob">@Lob</a>

Na Classe de inserção tem esse exemplo buscando o método getBytesFromFile

File arquivo = new File(“c:\test.jpg”);

int index = masterTable.getSelectedRow();
br.com.sistema.bean.PortariaVisitante pv = list.get(masterTable.convertRowIndexToModel(index));

try{

[color=red]pv.setFoto(getBytesFromFile(arquivo));[/color]

}catch(IOException e){}
//Método que transforma File em Bytes

public static byte[] getBytesFromFile(File file) throws IOException {

InputStream is = new FileInputStream(file);
// Get the size of the file
    long length = file.length();

    // You cannot create an array using a long type.
    // It needs to be an int type.
    // Before converting to an int type, check
    // to ensure that file is not larger than Integer.MAX_VALUE.
    if (length > Integer.MAX_VALUE) {
        // File is too large
    }

    // Create the byte array to hold the data
    byte[] bytes = new byte[(int)length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    // Ensure all the bytes have been read in
    if (offset < bytes.length) {
        throw new IOException("Could not completely read file "+file.getName());
    }

    // Close the input stream and return bytes
    is.close();
    return bytes;
}

Espero ter ajudado
Luiz Paulo Monteiro

Criado 29 de junho de 2009
Ultima resposta 26 de nov. de 2009
Respostas 3
Participantes 3