spring 如何在 Hibernate 中将 CLOB 数据类型转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11944530/
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
How to convert a CLOB data type to a String in Hibernate?
提问by Tiny
In hibernate (3.2.1.GA), I use the following method to insert CLOB type data into Oracle (10g) database.
在hibernate(3.2.1.GA)中,我使用如下方法将CLOB类型的数据插入到Oracle(10g)数据库中。
Hibernate.createClob(parameters.get("txtCatImage"));
parametersis a Mapwhere all the request parameters have been stored. While retrieving the Clobdata type from the database directly something like this entityObj.getCatImage()would not work.
parameters是Map存储所有请求参数的地方。在Clob直接从数据库中检索数据类型时,这样的操作entityObj.getCatImage()是行不通的。
Seen thisand thisquestions but couldn't find the way.
The following is the entity that uses a Clobtype property.
以下是使用Clob类型属性的实体。
public class Category implements java.io.Serializable {
private Long catId; // Primary key.
private Clob catImage; // CLOB type field.
// Other fields.
private static final long serialVersionUID = 1L;
public Category() {}
// Overloaded constructs + getters + setters + hashcode() + equals() + toString().
}
The Clobfield in the database just stores an image file name, in this case.
Clob在这种情况下,数据库中的字段仅存储图像文件名。
回答by Dev
Either call Clob.getSubString(long, int)with appropriate arguments to get the desired Stringor read the Clob as an InputStream or Reader using Clob.getAsciiStream()or Clob.getCharacterStream().
使用Clob.getSubString(long, int)适当的参数调用以获得所需的String或使用Clob.getAsciiStream()或将 Clob 作为 InputStream 或 Reader 读取Clob.getCharacterStream()。
If the Clob has fewer than 2147483647 (a.k.a. Integer.MAX_VALUE) characters you can do this
如果 Clob 少于 2147483647(又名Integer.MAX_VALUE)字符,您可以执行此操作
Clob clob = ... //Your clob
String clobString = clob.getSubString(0, clob.length());
回答by Axel
The solution from Dev (see above) to use "Clob.getSubString(long, int)" will be cost a lot of memory. I prefer the following solution.
Dev(见上文)使用“Clob.getSubString(long, int)”的解决方案将消耗大量内存。我更喜欢以下解决方案。
private static String getAsString(Clob clob) {
Reader reader = null;
BufferedReader bufferedReader = null;
try {
reader = clob.getCharacterStream();
bufferedReader = new BufferedReader(reader);
return IOUtils.toString(bufferedReader);
} catch (Exception e) {
throw new RuntimeException("Error while reading String from CLOB", e);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(bufferedReader);
}
}
回答by Shine
As Alex said, clob.getSubString(0, clob.length()) consumes memory and bad performance too. I had the same issue, where i am fetching a field of huge length varchar2(2000-4000 ) data from DB. The performance was slowing down just because of this varchar field to 1 min 20 secs. Fetching from Database to Entity classs was fast, but copying from Entity to SO object(or POJO) was taking huge time.
正如亚历克斯所说, clob.getSubString(0, clob.length()) 也消耗内存和糟糕的性能。我遇到了同样的问题,我从数据库中获取一个巨大长度的 varchar2(2000-4000) 数据字段。仅仅因为这个 varchar 字段,性能就会降低到 1 分 20 秒。从数据库到实体类的获取速度很快,但从实体到 SO 对象(或 POJO)的复制需要花费大量时间。
I am using JPA for DB data fetching. Entity field I kept as String. In SO Object(POJO) that field type I set as CLOB datatype.
我正在使用 JPA 来获取数据库数据。实体字段我保留为字符串。在 SO Object(POJO) 中,我将该字段类型设置为 CLOB 数据类型。
After fetching data from DB, the data is available in Entity object. To copy to SO object(CLOB type),
从 DB 获取数据后,数据在 Entity 对象中可用。复制到 SO 对象(CLOB 类型),
SOobject.setLongStringField( new SerialClob(entityString.toCharArray()));//Converting String to CLOB
And at UI level, my SOobject, instead of normal getter setter method , i used as below (Alex's code)
在 UI 级别,我的 SOobject,而不是普通的 getter setter 方法,我使用如下(Alex 的代码)
public String getLongStringField() {
Reader reader = null;
BufferedReader bufferedReader = null;
try {
reader = longStringField.getCharacterStream();
bufferedReader = new BufferedReader(reader);
return IOUtils.toString(bufferedReader);
} catch (Exception e) {
throw new RuntimeException("Error while reading String from CLOB", e);
} finally {
IOUtils.closeQuietly(reader);
IOUtils.closeQuietly(bufferedReader);
}
}
public void setLongStringField(Clob longStringField) {
this.longStringField= longStringField;
}
hope this helps!!!
希望这可以帮助!!!
回答by lmo
You can use this to read everything:
您可以使用它来阅读所有内容:
new BufferedReader(new InputStreamReader(clob.getAsciiStream())).lines().collect(Collectors.joining());
回答by ggDeGreat
This might work
这可能有效
new BufferedReader(new InputStreamReader(catImage.getAsciiStream())).readLine()

