我被迫使用hibernate createnativequery返回对象数组列表。查询返回值的(许多)列中有一列是clob。返回的对象是com.sun.proxy对象。我看到一个问题在哪里
getClass().getInterfaces()
用于标识它是正在返回的wrappedclob。但是,考虑到我现在在java代码中有了这个代理对象,如何将它转换成有用的东西。。。像一根绳子?
insrf1ej1#
以下代码有助于解除clob的固定链接帮助我,只是添加了一点化妆的代码: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. */ public static String unproxyClob(Object proxy) throws InvocationTargetException, IntrospectionException, IllegalAccessException, SQLException, IOException { try { BeanInfo beanInfo = Introspector.getBeanInfo(proxy.getClass()); for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { Method readMethod = property.getReadMethod(); if (readMethod.getName().contains(GET_WRAPPED_CLOB)) { Object result = readMethod.invoke(proxy); return clobToString((Clob) result); } } } catch (InvocationTargetException | IntrospectionException | IllegalAccessException | SQLException | IOException exception) { LOGGER.fatal(exception); throw exception; } return null; } /** * 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. */ public static String clobToString(Clob data) throws SQLException, IOException { StringBuilder sb = new StringBuilder(); Reader reader = data.getCharacterStream(); BufferedReader br = new BufferedReader(reader); String line; while (null != (line = br.readLine())) { sb.append(line); } br.close(); return sb.toString(); }
uoifb46i2#
你可以试试这个代码。它可能并不完美,但对我来说很有用:)我正在使用它将常规clob转换为字符串,因此如果使用getwrappedclob()展开clob对象,它也应该可以工作
Clob clob = wrappedClob.getWrappedClob(); String result = null; Long length = clob.length(); char[] char_array = new char[length.intValue()]; Reader characterStream = clob.getCharacterStream(); try { int read = characterStream.read(char_array, 0, length.intValue()); result = new String(char_array); } catch (IOException e) { log.error("Exception during read from CLOB: " + e); }
2条答案
按热度按时间insrf1ej1#
以下代码有助于解除clob的固定
链接帮助我,只是添加了一点化妆的代码:d
uoifb46i2#
你可以试试这个代码。它可能并不完美,但对我来说很有用:)我正在使用它将常规clob转换为字符串,因此如果使用getwrappedclob()展开clob对象,它也应该可以工作