Tipo Blob em um pojo, serializacao [+ajuda][+findbugs]

0 respostas
rollei

Estava eu dando uma olhada num pojo de uma aplicação que tem arquivos anexos como uma propriedade. Este arquivo anexo é do tipo java.sql.Blob … passei o findbugs nesse pojo e ele está me retornando um aviso:

“This Serializable class defines a non-primitive instance field which is neither transient, Serializable, or java.lang.Object, and does not appear to implement the Externalizable interface or the readObject() and writeObject() methods. Objects of this class will not be deserialized correctly if a non-Serializable object is stored in this field.”

Esse Blob é persistido junto com a entidade no BD, nesse caso qual seria a melhor maneira de concertar esse aviso?

EDIT
Sim, dentro do pojo ele, alem de ter colocado o blob colocou um campo File e criou dois metodos:

public void setArquivo(File arquivo) throws IOException {
		FileInputStream fis = null;
		try {
			this.arquivo = arquivo;
			fis = new FileInputStream(arquivo);
			byte[] theBytes = new byte[(int) arquivo.length()];
			
			int bytesRead = fis.read(theBytes);
			
			if (bytesRead != theBytes.length) {
				// TODO colocar uma mensagem de excecao
				throw new IOException("");
			}
			
			docAnexo = Hibernate.createBlob(theBytes);
		} finally {
			if (fis != null) {
				fis.close();
			}
		}
	}
public static File converteBlobParaFile(Blob blob, String caminho)
			throws SQLException, IOException {
		BufferedInputStream input = null;
		BufferedOutputStream output = null;
		
		try {
			input  = new BufferedInputStream(blob.getBinaryStream());
			output = new BufferedOutputStream(new FileOutputStream(caminho));
			
			int i;
			while ((i = input.read()) != -1) {
				output.write(i);
			}
			File arquivo = new File(caminho);
			
			return arquivo;
		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} finally {
				if (input != null) {
					input.close();
				}
			}
		}
	}
Criado 9 de setembro de 2009
Respostas 0
Participantes 1