Hibernate Createnativequery Returning Proxy Object For Clob
Solution 1:
The following code helped to unProxy the clob
Link which help me, just add a bit of makeup to the code :D
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Clob;
import java.sql.SQLException;
/**
* Unproxy clob.
*
* @param proxy the proxy
* @return the string
* @throws InvocationTargetException the invocation target exception
* @throws IntrospectionException the introspection exception
* @throws IllegalAccessException the illegal access exception
* @throws SQLException the SQL exception
* @throws IOException Signals that an I/O exception has occurred.
*/publicstatic String unproxyClob(Object proxy)throws InvocationTargetException, IntrospectionException, IllegalAccessException, SQLException, IOException {
try {
BeanInfobeanInfo= Introspector.getBeanInfo(proxy.getClass());
for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
MethodreadMethod= property.getReadMethod();
if (readMethod.getName().contains(GET_WRAPPED_CLOB)) {
Objectresult= readMethod.invoke(proxy);
return clobToString((Clob) result);
}
}
} catch (InvocationTargetException | IntrospectionException | IllegalAccessException | SQLException | IOException exception) {
LOGGER.fatal(exception);
throw exception;
}
returnnull;
}
/**
* Clob to string.
*
* @param data the data
* @return the string
* @throws SQLException the SQL exception
* @throws IOException Signals that an I/O exception has occurred.
*/publicstatic String clobToString(Clob data)throws SQLException, IOException {
StringBuildersb=newStringBuilder();
Readerreader= data.getCharacterStream();
BufferedReaderbr=newBufferedReader(reader);
String line;
while (null != (line = br.readLine())) {
sb.append(line);
}
br.close();
return sb.toString();
}
Solution 2:
You can try this code. Probably it is not perfect, but it works for me :) I'm using it to convert regular clob to string so it should also work if you unwrap clob object with getWrappedClob()
Clobclob= wrappedClob.getWrappedClob();
Stringresult=null;
Longlength= clob.length();
char[] char_array = newchar[length.intValue()];
ReadercharacterStream= clob.getCharacterStream();
try {
intread= characterStream.read(char_array, 0, length.intValue());
result = newString(char_array);
} catch (IOException e) {
log.error("Exception during read from CLOB: " + e);
}
Solution 3:
This is not a direct answer to OP’s question, as I did not have to deal with an Instance of ‘WrappedClob’. But as I still got the same misleading error message, I hope someone finds this useful:
The problem for me was, that hibernates implementation of ‘Clob to String’ for H2 and Postgres did not behave the same way. So, while my tests worked fine, the application would crash at runtime. To work around this, I instantiated different beans of ClobReader in different environments. The production environment would us the bean implementation for Postgres, while the test code uses the H2 bean.
publicinterfaceClobReader {
String clobToString(Object clob)throws SQLException, IOException;
}
@Beanpublic ClobReader clobReaderPostgres() {
returnnewClobReader() {
@Overridepublic String clobToString(final Object clob) {
if (clob == null) {
returnnull;
}
return clob.toString();
}
};
}
@Bean@Primarypublic ClobReader clobReaderH2() {
returnnewClobReader() {
@Overridepublic String clobToString(final Object obj)throws SQLException, IOException {
finalClobclob= (Clob) obj;
try (finalReaderreader= clob.getCharacterStream()) {
try (finalStringWriterstringWriter=newStringWriter()) {
IOUtils.copy(reader, stringWriter);
return stringWriter.toString();
}
}
}
};
}
Post a Comment for "Hibernate Createnativequery Returning Proxy Object For Clob"