2015-03-11 1 views
1

Je suis forcé dans une situation où j'utilise hibernate createNativeQuery pour retourner une liste de tableaux d'objets.Hibernate createNativeQuery retourne l'objet Proxy pour Clob

L'une des (nombreuses) colonnes à partir de laquelle ma requête renvoie des valeurs est un CLOB.

L'objet renvoyé est objet com.sun.Proxy.

Je l'ai vu une question here

getClass().getInterfaces() 

a été utilisé pour identifier qu'il est un WrappedClob retourné.

Cependant, étant donné que j'ai maintenant cet objet proxy dans mon code Java, comment puis-je le convertir en quelque chose d'utile ... comme une chaîne?

Répondre

0

Vous pouvez essayer ce code. Il est probablement pas parfait, mais ça marche pour moi :) Je l'utilise pour convertir clob régulière en chaîne il devrait également fonctionner si vous déballez objet clob avec getWrappedClob()

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

Le code suivant aidé à la unProxy 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. 
     */ 
     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(); 
     } 
+0

Je pense que vous avez oublié d'ajouter cet attribut à vous classe 'static String GET_WRAPPED_CLOB = "getWrappedClob"'. Mais :-)!!! –