我想通过 Hibernate 使用 BLOB 列类型将文件插入到 Oracle?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1243706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 18:45:45  来源:igfitidea点击:

I want to insert a file to Oracle using BLOB column type over Hibernate?

javaoraclehibernate

提问by firstthumb

I want to save my file on Oracle Instance. I am using Hibernate for data objects. How can I insert files into Oracle Blob. Is there any sample code for this?

我想将我的文件保存在 Oracle 实例上。我正在将 Hibernate 用于数据对象。如何将文件插入 Oracle Blob。是否有任何示例代码?

回答by Vanger

In first you must annotate your entity field with @javax.persistance.Lob annotation. Like this:

首先,您必须使用 @javax.persistance.Lob 注释来注释您的实体字段。像这样:

public class InfoMessage {

    private byte[] body;

    @Lob
    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }
}

and set it with bytes array. It's depends on wich File class you use. The first google result for java.io.File. I guess there's better solution for this operation.

并用字节数组设置它。这取决于您使用的 File 类。java.io.File 的第一个谷歌结果。我想这个操作有更好的解决方案。

public static byte[] getBytesFromFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);
// Get the size of the file
long length = file.length();

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;

}

}

回答by Vanger

The @Lobannotation is not Hibernate's one. It's javax.persistenceand you can use it in entity bean with any hibernate mapping. Yes, the big file is obvious problem for such example. But i can't find any workaround for this case. FileInputStreamusing int type value to represent offset point. I googled this one with similiar problem: http://www.coderanch.com/t/449055/Streams/java/JAVA-HEAP-SIZE-files-byteYou can use solution with SQLPrepareSteatementif you use Java 1.6. Othwerwise you can try to use BufferedReaderand somehow convert results to byteArray[]and try to beat another problem: you'll need so much memory as file size is.

@Lob注解并非是Hibernate的一个。它是javax.persistence,您可以在具有任何休眠映射的实体 bean 中使用它。是的,对于这样的例子,大文件是明显的问题。但是我找不到针对这种情况的任何解决方法。FileInputStream使用 int 类型值来表示偏移点。我用类似的问题搜索了这个:http: //www.coderanch.com/t/449055/Streams/java/JAVA-HEAP-SIZE-files-byteSQLPrepareSteatement如果您使用 Java 1.6,您可以使用解决方案。否则,您可以尝试使用BufferedReader并以某种方式将结果转换为byteArray[]并尝试解决另一个问题:您将需要与文件大小一样多的内存。

EDITED:Another thing: Oracle can append data to it's clob\blob fields using dbms_lob.writeappend() procedure so you can avoid having all file in memory, but will GC perform clean as fast as BufferedReaderread from file. And seems it's not a hibernate work to do this... jdbc and PreparedStatements back again.

编辑:另一件事:Oracle 可以使用 dbms_lob.writeappend() 过程将数据附加到它的 clob\blob 字段,这样您就可以避免将所有文件都放在内存中,但 GC 执行清理的速度会与BufferedReader从文件读取的速度一样快。并且似乎它不是一个休眠的工作来做这个...... jdbc 和 PreparedStatements 又回来了。